#XPRIM_Json Question

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
soa
Posts: 339
Joined: Mon Dec 07, 2015 3:15 pm

#XPRIM_Json Question

Post by soa »

I am using a json service which returns an array (see below). I don't know how many entries are in the array.

How do I iterate through these elements and extract the data?

[
{
"id":1,
"title":"numeracy",
"version":"1",
},
{
"id":3,
"title":"Minimum Standards Numeracy",
"version":"17",
},
{
"id":4,
"title":"Minimum Standards Writing (Group A)",
"version":"19",

},
{
"id":5,
"title":"Minimum Standards Reading",
"version":"20",

},
{
"id":6,
"title":"Minimum Standards Writing (Group B)",
"version":"21",

},
{
"id":7,
"title":"Minimum Standards Writing (Group C)",
"version":"22",

}
]
tsupartono

Re: #XPRIM_Json Question

Post by tsupartono »

Jim, please find an example below how to iterate through an array element.
The code assumes the RootArray points to your root array, and that each element in the array is an object.

Code: Select all

Define_Com Class(#STD_INT) Name(#i)
Define_Com Class(#XPRIM_JsonObject) Name(#Item) Reference(*DYNAMIC)

Begin_Loop Using(#i) To(#RootArray.ItemCount)
   #Item <= #RootArray.ItemAt<#i> *As #XPRIM_JsonObject
End_Loop
Keep in mind that there is an EPC coming up soon for 14 SP1 that includes a new JsonReader component, which is designed to read big JSON objects more efficiently. It is not going to make much difference if you have only a few items in your JSON, but it definitely will if you have 100s or more.
soa
Posts: 339
Joined: Mon Dec 07, 2015 3:15 pm

Re: #XPRIM_Json Question

Post by soa »

Thanks Tony, I'll give that a go.
soa
Posts: 339
Joined: Mon Dec 07, 2015 3:15 pm

Re: #XPRIM_Json Question

Post by soa »

I see now that the JsonReader component but the documentation stops at RandomAccessJsonreader. I'm after an example of reading through a large array.

Cheers
Jim
tsupartono

Re: #XPRIM_Json Question

Post by tsupartono »

HI Jim,
Please see below a JsonReader example of parsing a JSON array containing objects like so:

Code: Select all

[
   {"value": 1}, 
   {"value": 2}
   . . .
]
The example below uses SetSourceString to set the source string. Replace that with SetSourceHttpResponse if you are parsing a HTTP response data.

Code: Select all

Define_Com Class(#XPRIM_JsonReader) Name(#Reader)
Define_Com Class(#PRIM_BOLN) Name(#OK)
Define_Com Class(#XPRIM_JsonObject) Name(#JsonObj) Reference(*DYNAMIC)

#Reader.SetSourceString String('[{"value": 1}, {"value": 2}]')

#Reader.ReadBeginArray Found(#OK)
#Reader.MoveNext Ok(#OK)

Dowhile Cond(#OK *And (*Not #Reader.IsEndArray))
    #Reader.ReadObject Found(#OK) Result(#JsonObj)
    #Reader.MoveNext Ok(#OK)

    #STD_NUM := #JsonObj.GetNumber('value')
Endwhile

If (*Not #OK)
    * Something is wrong..... 
Endif
soa
Posts: 339
Joined: Mon Dec 07, 2015 3:15 pm

Re: #XPRIM_Json Question

Post by soa »

Thanks, Tony I'll give that a go.
Post Reply