JsonObject - JsonArray

Posted by goo on 08-Nov-2017 07:17

I have started diving into Json and have a question.

This is the json respons:

{

"@odata.context":"utviklingcrm.crm4.dynamics.com/.../$metadata
{
"@odata.etag":"W/\"6571238\"","contactid":"1755f448-e198-e711-80ef-3863bb349b40"
}
]
}

This cString = oDyn365:oObject:GetJsonText('value').

gives me:

[
{
"@odata.etag":"W/\"6571238\"","contactid":"1755f448-e198-e711-80ef-3863bb349b40"
}
]

I believe the content of the oObject is an array. How do I add it to an array? If I would like to do:

cString = oDyn365:oObject:GetJsonText('contactid').

do I have to add the result of oDyn365:oObject:GetJsonText('value') into another oObject ?

//Geir Otto

Posted by goo on 08-Nov-2017 07:46

Sorry, I found the answare within Progress documentation...  a bit too quick :-)

oArray = oObject:getJsonArray('value').

All Replies

Posted by goo on 08-Nov-2017 07:46

Sorry, I found the answare within Progress documentation...  a bit too quick :-)

oArray = oObject:getJsonArray('value').

Posted by Akshay Guleria on 08-Nov-2017 08:03

First, the json response you have posted in not a valid JSON. You can use any tool like [View:https://jsoncompare.com/:550:50] to validate a JSON.

Second, below is an example of probably what can help you. Look out for documentation of JsonObject and JsonArray classes and you will find it very easy to understand what else you can do with them. 

USING Progress.Json.ObjectModel.*.
 
DEF VAR myObject AS JsonObject NO-UNDO.
DEF VAR myData AS JsonObject NO-UNDO.
DEF VAR myParams AS JsonObject NO-UNDO.
DEF VAR lResult AS LONGCHAR NO-UNDO
    VIEW-AS EDITOR LARGE SIZE 60 BY 16 LABEL "Json Object".   
DEF VAR lJsArray AS JsonArray NO-UNDO.
DEF VAR lAnotherArray AS JsonArray NO-UNDO.
 
/* main json objects/instances */
myObject = NEW JsonObject().          
myParams = NEW JsonObject().
 
/* add data to main json object */
myObject:Add("type", "customer").
myObject:Add("id", 348289).
 
/* add data to params/sub json object */
myParams:Add("first name", "Zingo Singh").
myParams:Add("balance", 430.56).
myParams:Add("active", TRUE).
myParams:Add("street address", "Kalliotie 11").
myParams:Add("city","Very Shitty").
myParams:Add("zipcode", "07218").
myParams:Add("country", "Finland").
 
lJsArray = NEW JsonArray().
lJsArray:Add(myParams).
lAnotherArray = NEW JsonArray().
lAnotherArray:Add("Array wihtout object").
lJsArray:Add(lAnotherArray).
myObject:Add("data", lJsArray).
 
/* write json object to variable for output and param TRUE formats the json for display*/
myObject:WRITE(lResult, TRUE).
 
DISP lResult.

Json Object
------------------------------------------------------------
{
"type": "customer",
"id": 348289,
"data": [
{
"first name": "Zingo Singh",
"balance": 430.56,
"active": true,
"street address": "Kalliotie 11",
"city": "Very Shitty",
"zipcode": "07218",
"country": "Finland"
},
[
"Array wihtout object"
]
]
}

This thread is closed