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 Posts: 95Member
    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;
    
    Arturas Kulsinskas
    NaviLogic
  • navuser1navuser1 Posts: 1,315Member
    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 Posts: 95Member
    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
    Arturas Kulsinskas
    NaviLogic
  • navuser1navuser1 Posts: 1,315Member
    Thanks a lot. I will it and get back to you.
    Now or Never
  • navuser1navuser1 Posts: 1,315Member
    edited 2020-02-14
    AddJObjectToJObject function missing in my cu 5459
    Now or Never
  • navuser1navuser1 Posts: 1,315Member
    Superb. It's working fine.

    Thanks a lot.
    Now or Never
Sign In or Register to comment.