SBX - Search With Button

SBX - Forum Post Title

How to iterate over JSON array in Navison 2018 using C/AL

Microsoft Dynamics NAV Forum

NeoMatrix asked a question on 11 Jun 2019 1:48 PM

Question Status

Verified

Hi All,

I need to iterate over JSONArray in Nav2018, following is the JSON Array snippet.

JSON Data:

{
"return": [
{
"id": 1,
"first_name": "aaa",
"last_name": "ddd",
},
{
"id": 2,
"first_name": "bbb",
"last_name": "eee",
},
{
"id": 3,
"first_name": "ccc",
"last_name": "ttt",
}
]
}

Here I need to insert each "id" data like id, first_name,last_name as a row in the database table.

Thanks in advance.

Reply
I Gusti Made Ari Permadi responded on 11 Jun 2019 9:56 PM
My Badges
Verified Answer

Hi, you can use codeunit JSONManagement to do that .

Sample code :

//Create JSONArray String
JSONManagement.InitializeFromString(JsonString); JSONManagement.GetJSONObject(JObject); ArrayString := JObject.SelectToken('return').ToString;
//Read and Parse JSONArray CLEAR(JSONManagement); CLEAR(JObject); JSONManagement.InitializeCollection(ArrayString); JSONManagement.GetJsonArray(JSONArray);
//loop and can do insert FOREACH JObject IN JSONArray DO BEGIN MESSAGE('%1 %2 %3', JObject.GetValue('id') , JObject.GetValue('first_name'), JObject.GetValue('last_name') ); END; Name DataType Subtype Length JSONManagement Codeunit JSON Management JSONArray DotNet Newtonsoft.Json.Linq.JArray.'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' JObject DotNet Newtonsoft.Json.Linq.JObject.'Newtonsoft.Json' ArrayString Text


Reply
mimran responded on 12 Jun 2019 12:23 AM

Thanks, This is helpful.

Reply
NeoMatrix responded on 12 Jun 2019 1:14 PM
Suggested Answer

Thank for the Reply. It's works perfectly.

Reply
NeoMatrix responded on 12 Jun 2019 1:17 PM
Suggested Answer

thanks

Reply
I Gusti Made Ari Permadi responded on 11 Jun 2019 9:56 PM
My Badges
Verified Answer

Hi, you can use codeunit JSONManagement to do that .

Sample code :

//Create JSONArray String
JSONManagement.InitializeFromString(JsonString); JSONManagement.GetJSONObject(JObject); ArrayString := JObject.SelectToken('return').ToString;
//Read and Parse JSONArray CLEAR(JSONManagement); CLEAR(JObject); JSONManagement.InitializeCollection(ArrayString); JSONManagement.GetJsonArray(JSONArray);
//loop and can do insert FOREACH JObject IN JSONArray DO BEGIN MESSAGE('%1 %2 %3', JObject.GetValue('id') , JObject.GetValue('first_name'), JObject.GetValue('last_name') ); END; Name DataType Subtype Length JSONManagement Codeunit JSON Management JSONArray DotNet Newtonsoft.Json.Linq.JArray.'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' JObject DotNet Newtonsoft.Json.Linq.JObject.'Newtonsoft.Json' ArrayString Text


Reply
NeoMatrix responded on 12 Jun 2019 1:14 PM
Suggested Answer

Thank for the Reply. It's works perfectly.

Reply
NeoMatrix responded on 12 Jun 2019 1:17 PM
Suggested Answer

thanks

Reply

SBX - Two Col Forum

SBX - Migrated JS