Question
· Aug 30, 2017

Retrieving values from JSON %Library.DynamicObject (2017.1)

Good afternoon - 

I'm in the process of learning to make COS calls to REST-based web services and while I am having success, I'm struggling on how to parse out the results (I admit I'm very green at this):

Here's the code retrieving the JSON response:

w !,"Get the JSON"
Set Result = {}.%FromJSON(Request.HttpResponse.Data)

This has Result as a Library.DynamicObject.

I can then write out the response using:

w !,Result.%ToJSON()

This works, I can see the response is valid and what I want. Here is the JSON returned (de-identified):
 

{
  "RecordIDs": [
    {
      "ID": "1234",
      "Type": "INTERNAL"
    },
    {
      "ID": "1234",
      "Type": "EXTERNAL"
    }
  ],
  "ContactIDs": null,
  "Items": [
    {
      "ItemNumber": "320",
      "Value": null,
      "Lines": [
        {
          "LineNumber": 0,
          "Value": "1",
          "Sublines": null
        },
        {
          "LineNumber": 1,
          "Value": "100063287",
          "Sublines": null
        }
      ]
    }
  ]
}

What is the easiest method to get the LineNumber Values out? I could potentially have several so I'm trying to get an output kind of like Value1~Value2~Value3 so I can piece them out later.

Appreciate any advice - I'll keep pluggin away on it. 

Discussion (4)1
Log in or sign up to continue
set obj = {"RecordIDs":[{"ID":"1234","Type":"INTERNAL"},{"ID":"1234","Type":"EXTERNAL"}],"ContactIDs":null,"Items":[{"ItemNumber":"320","Value":null,"Lines":[{"LineNumber":0,"Value":"1","Sublines":null},{"LineNumber":1,"Value":"100063287","Sublines":null}]}]}
w obj.Items.%Get(0).Lines.%Get(0).LineNumber
>0
w obj.Items.%Get(0).Lines.%Get(1).LineNumber
>1

To iterate over arbitrary number of array elements use iterator:

set iterator =  obj.Items.%Get(0).Lines.%GetIterator()
while iterator.%GetNext(.key,.line) { w line.LineNumber,! }
>0
>1