Trouble extracting JSON when outer object omits name

Posted by Rod Anderson on 08-Aug-2017 08:57

I'm calling a rest service and getting back the below JSON. I tried to build a dynamic dataset but since the the JSON document omits the outer object I get errors 15358 and 15374 which I can't seem to find a work around. 

I then tried picking the specific data I need using:

oObject = CAST(oEntity, JsonObject).
oNestObject1 = oObject:GetJsonObject(""). /* this should be the name of the outer object */
cRequestID = oNestObject1:GetJsonText('requestId').

But without the name of the outer object I can't grab the value(s). Any ideas as to how to resolve this?

Thanks in advance,

Rod



{
"requestId": "f9f471c9-4c3f-403a-86e7-aadae573350f",
"application": "XCDS",
"genDt": "",
"createDt": "2017-08-07T14:35:14.931-04:00",
"timeToLive": 60,
"runAfterDate": "",
"runAfterRequestId": null,
"fixId": null,
"fixOption": null,
"status": "Maintenance never sent",
"sent": false,
"_links": [{
"rel": "appList",
"uri": "http:\/\/smisdev.mydomain.com\/polling\/v1\/app\/XCDS",
"methods": [
"GET",
"POST",
"OPTIONS"
]
},
{
"rel": "operations",
"uri": "http:\/\/smisdev.mydomain.com\/polling\/v1\/f9f471c9-4c3f-403a-86e7-aadae573350f\/operations",
"methods": [
"GET",
"POST",
"DELETE",
"OPTIONS"
]
},
{
"rel": "self",
"uri": "http:\/\/smisdev.mydomain.com\/polling\/v1\/f9f471c9-4c3f-403a-86e7-aadae573350f",
"methods": [
"GET",
"POST",
"PUT",
"OPTIONS"
]
},
{
"rel": "stores",
"uri": "http:\/\/smisdev.mydomain.com\/polling\/v1\/f9f471c9-4c3f-403a-86e7-aadae573350f\/stores",
"methods": [
"GET",
"PUT",
"POST",
"DELETE",
"OPTIONS"
]
}
]
}

All Replies

Posted by onnodehaan on 08-Aug-2017 09:08

Hi

Try reading it in as an JsonArray instead of an JsonObject

Posted by Rod Anderson on 08-Aug-2017 10:36

Ok I give up being stupd.

if TYPE-OF(oEntity, JsonObject) equates to true when it comes back from the REST service then how to I transform that to a JsonArray without out writing it to a file and reading it back in?

Posted by Tim Kuehn on 08-Aug-2017 10:44

The array'll have a name - you can get it that way. Here's an example from the 11.6 docs -

DEFINE VARIABLE CustInfo AS LONGCHAR NO-UNDO.
DEFINE VARIABLE myObj AS JsonObject NO-UNDO.
DEFINE VARIABLE myArray AS JsonArray NO-UNDO.

myObj = CAST(myParser:Parse(CustInfo), JsonObject).
myArray = myObj:GetJsonObject("CustOrder"):GetJsonArray("ttOrder").

Posted by marian.edu on 08-Aug-2017 11:24

You can’t transform it to an array, unless you put that object inside an array but what would be the gain of this?


You already have the object, there isn’t any nested object there… you can use the properties names directly, there is an nested array thought (‘_links’) so there you will have to work with a JsonArray - you can retrieve that with GetJsonArray(‘_links’).

Marian Edu

Acorn IT 
+40 740 036 212

Posted by Rod Anderson on 08-Aug-2017 11:36

Tim,  Thank you for the responce.  I looked at that example previoulsy but it has a  outer object called CustOrder and an inner called ttOrder.  I don't have anything to reference (see original json repsonce).   Having said that it's problably my ignorance so please feel free to educate away:)

Posted by Tim Kuehn on 08-Aug-2017 11:40

use the oJsonObject:GetNames( ) aPI to get a list of names of items it contains. You can then use oJsonObject:GetType() to find out the type of each name.

See the JsonObject docs for more details.

Posted by Rod Anderson on 08-Aug-2017 12:18

The lightbulb finally came on.  Thanks to everyone.

This thread is closed