JSON Management, nested JObjects

Dear everyone,

i have a problem to loop through nested JObjects within the Json Management
{
"entries": {
"10000": {
"product": "A",
"pricePerUnit": 5.68,
},
"20000": {
"product": "B",
"pricePerUnit": 5.83,
},
"30000": {
"product": "A",
"pricePerUnit": 5.53,
}
},
"currency": "EUR"
}

At the moment we used the following code to run through the JObject, but it is not working:
JSONManagement.InitializeObject(Json);
JSONManagement.GetJSONObject(JObject);
JSONManagement.GetArrayPropertyValueFromJObjectByName(JObject,'Customer',JsonArray);
FOR Index := 1 TO JsonArray.Count DO BEGIN
JCustObject := JsonArray.Item(Index - 1);
JSONManagement.GetStringPropertyValueFromJObjectByName(JCustObject,'No',Value);
TempCustomer."No." := Value;
TempCustomer.INSERT;
END;
I've tried to use the Jobject.GetValue command but I'll always recieve an error message


How can i correctly loop through the Jobject with the Json Management.

Answers

  • ShaiHuludShaiHulud Member Posts: 228
    edited 2019-04-05
    The JSON you attached doesn't really match the code, but these modifications (based on NAV 2018 JSONManagement codeunit) should do the trick
    JSONManagement.InitializeObject(Json);
    JSONManagement.GetJSONObject(JObject);
    JSONManagement.GetArrayPropertyValueFromJObjectByName(JObject,'Customer',JsonArray);
    JSONManagement.InitializeCollectionFromJArray(JsonArray); //declare collection
    FOR Index := 1 TO JsonArray.Count DO BEGIN
      JSONManagement.GetJObjectFromCollectionByIndex(JCustObject, Index-1); //"extract" a single JObject from the collection
      JSONManagement.GetPropertyValueFromJObjectByName(JCustObject,'No',Value); //read the value
      TempCustomer."No." := Value;
      TempCustomer.INSERT;
    END;
    
  • navuser1navuser1 Member Posts: 1,329
    edited 2020-02-14
    Hi Arturas,

    I need some idea to create the above JSON file thru C/AL. Do you have some time to share ?
    Now or Never
  • ShaiHuludShaiHulud Member Posts: 228
    navuser1 wrote: »
    Hi Arturas,

    I need some idea to create the above JSON file thru C/AL. Do you have some time to share ?

    The bellow JSON could be made with a code like (keep in mind it's not tested!) this:
    {
        "entries": [
            {
              "10000": {
                  "product": "A",
                  "pricePerUnit": 5.68
              }
            },
            {
                "20000": {
                  "product": "B",
                  "pricePerUnit": 5.83
              }
            },
            {
            "30000": {
                "product": "A",
                "pricePerUnit": 5.53
              }
            }
        ],
        "currency": "EUR"
    }
    
    MainJSON := MainJSON.JObject;
    EntriesJSONArray := EntriesJSONArray.JArray;
    IF RecEntry.FINDSET THEN BEGIN
      REPEAT
        CLEAR(EntryJSON);
        CLEAR(SubEntryJSON);
        EntryJSON := EntryJSON.JObject;
        SubEntryJSON := SubEntryJSON.JObject;
        JSONMgt.AddJPropertyToJObject(SubEntryJSON, 'Product', RecEntry.Product);
        JSONMgt.AddJPropertyToJObject(SubEntryJSON, 'Product', RecEntry."Price Per Unit");
    
        JSONMgt.AddJObjectToJObject(EntryJSON, FORMAT(RecEntry."Line No."), SubEntryJSON);
        JSONMgt.AddJObjectToJArray(EntriesJSONArray, EntryJSON);
      UNTIL RecEntry.NEXT = 0;
    END;
    
    JSONMgt.AddJArrayToJObject(MainJSON, 'entries', EntriesJSONArray);
    JSONMgt.AddJPropertyToJObject(MainJSON, 'currency', 'EUR');
    

    Here:
    MainJSON, EntryJSON and SubEntryJSON are of subtype "Newtonsoft.Json.Linq.JObject.'Newtonsoft.Json'"
    EntriesJSONArray of subtype "Newtonsoft.Json.Linq.JArray.'Newtonsoft.Json'"
    And JSONMgt is Codeunit 5459 - JSON Management
  • navuser1navuser1 Member Posts: 1,329
    edited 2020-02-24
    Thanks a lot. I will check it and get back to you.
    Now or Never
  • navuser1navuser1 Member Posts: 1,329
    edited 2020-02-14
    AddJObjectToJObject function missing in my cu 5459
    Now or Never
  • navuser1navuser1 Member Posts: 1,329
    Superb. It's working fine.

    Thanks a lot.
    Now or Never
  • julkifli33julkifli33 Member Posts: 1,092
    hi ..
    is there a way for this nested json but for cloud environment?
  • dreezdreez Member Posts: 72
    julkifli33 wrote: »
    hi ..
    is there a way for this nested json but for cloud environment?

    Sure there is, here is just an example, I didn't go through records like @ShaiHulud, but I believe you get the idea:
    local procedure TestJSON()
    var
        JSONManagement: Codeunit "JSON Management";
        MainJObject: JsonObject;
        EntriesJArray: JsonArray;
        FirstEntryObject: JsonObject;
        FirstEntryExampleObject: JsonObject;
        MainJObjectAsString: Text;
    begin
        FirstEntryExampleObject.Add('product', 'A');
        FirstEntryExampleObject.Add('priceperUnit', 5.68);
        FirstEntryObject.Add('10000', FirstEntryExampleObject);
        EntriesJArray.Add(FirstEntryObject);
        MainJObject.Add('entries', EntriesJArray);
        MainJObject.Add('currency', 'EUR');
        MainJObject.WriteTo(MainJObjectAsString);
        JSONManagement.InitializeObject(MainJObjectAsString); // Just to format JSON nicely. Not necessary
        Message(JSONManagement.WriteObjectToString()); // To test if it works.
    end;
    
Sign In or Register to comment.