skip to Main Content

I’m having trouble reading a JSON that has a extent[6] data property.
This is the JSON:

{  
    "SalesRep": [  
        {  
            "SalesRep": "BBB",  
            "RepName": "Brawn , Bubba B.",  
            "Region": "East",  
            "MonthQuota": [  
                1600,1648,1697,1748,1800,1854  
            ]  
        }  
    ]  
}

I want to read the second numerical value (extent[2]=1648).

This is my pgm:

myParser    = NEW ObjectModelParser( ).  
myConstruct = myParser:ParseFile("JsonRaro.json").  
myJsonObj2 = CAST(myJsonObj1:GetJsonArray("ttSalesRep"):GetJsonObject(1),JsonObject).  
MESSAGE  
    "2 " STRING( myJsonObj2:GetJsonText("RepName") ) SKIP /*result="2 Brawn , Bubba B."*/  
    "3 " STRING( myJsonObj2:GetJsonText("MonthQuota") ) SKIP /*result="
                                                          [1600,1648,1697,1748,1800,1854]"*/  
    "3a " STRING( myJsonObj2:GetJsonArray("MonthQuota"):GetJsonArray(2,1) ) SKIP /*ERROR*/  
VIEW-AS ALERT-BOX.

Message result

2

Answers


  1. You only need to get the item from the array, not another array.

    So this

     "3a " STRING( myJsonObj2:GetJsonArray("MonthQuota"):GetJsonArray(2,1) )
    

    should be

     "3a " STRING( myJsonObj2:GetJsonArray("MonthQuota"):GetInteger(2) )
    
    Login or Signup to reply.
  2. I see your are using a JsonObject, which can do the trick and is very flexible.

    If the structure of the json is simple and is valid to map to a temp-table, you can use something like:

    define temp-table ttSalesRep serialize-name "SalesRep"
      field SalesRep   as character
      field RepName    as character
      field Region     as character
      field MonthQuota as integer extent 12.  // twelve months
    
    temp-table ttSalesRep:read-json("file", "JsonRaro.json", "empty").
    
    find ttSalesRep.
    display ttSalesRep.MonthQuota[2].
    

    The downside is that you need to define field MonthQuota with a fixed extend number (but I guess 12 is enough).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search