JSON Management, nested JObjects

Badaboomchef
Member Posts: 1
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.
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.
0
Answers
-
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;
0 -
Hi Arturas,
I need some idea to create the above JSON file thru C/AL. Do you have some time to share ?Now or Never0 -
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 Management2 -
Thanks a lot. I will check it and get back to you.Now or Never0
-
AddJObjectToJObject function missing in my cu 5459Now or Never0
-
Superb. It's working fine.
Thanks a lot.Now or Never0 -
hi ..
is there a way for this nested json but for cloud environment?0 -
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;
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions