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.
To iterate over arbitrary number of array elements use iterator:
Awesome, this is exactly what I was looking for! Thank you. I was getting close with %Get and the iterator but wasn't quite making the connection with the whole line.LineNumber part.
Thanks!!!
Thanks for your answer as well! This is actually what we were already doing with 2015.1 when handling JSON. So moving into 2017.1, I was hoping to make more use of the built-in JSON handlers because the JSON response I posted was a rather simple one - some of our more complex responses has our string manipulations getting quite long-in-the-tooth.
I'd suggest in this case to take the original JSON String from Request.HttpResponse.Data
and split it by "LineNumber":
eg:
for line =2:1:$l(json,sep) set line(line)=+$p(json,sep,line)
and you get
line=3
line(2)=0
line(3)=1
I admit it's not very object-ish but efficient