web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

Parse Nested JSON

(0) ShareShare
ReportReport
Posted on by 115

I need to parse the following JSON, i have been able to parse each object into a JSon Array, but i am having trouble parsing the TransactionItems into an array. TransactionItem are Sales Lines i need to loop through the TransactionItem and create a sales line for each object in there.

[
{
"Id": 314324236316890,
"CustomerId": null,
"StaffId": 10922343821,
"TransactionItems": [
{
"Id": 1051958756,
"TransactionId": 316334316890,
"ProductId": 1673432427092,
"UnitPrice": 1.2500000000000000,
"UnitPriceExcTax": 1.25000343400000000000,
"CostPrice": 34334
},
{
"Id": 1051958757,
"TransactionId": 343434340,
"ProductId": 167343427096,
"UnitPrice": 1.3503340000000000000,
"UnitPriceExcTax": 1.3500343000000000000,
"CostPrice": 0.0000000034300000000,
}
]
},

{
"Id": 314324236316890,
"CustomerId": null,
"StaffId": 10922343821,
"TransactionItems": [
{
"Id": 1051958756,
"TransactionId": 316334316890,
"ProductId": 1673432427092,
"UnitPrice": 1.2500000000000000,
"UnitPriceExcTax": 1.25000343400000000000,
"CostPrice": 34334
},
{
"Id": 1051958757,
"TransactionId": 343434340,
"ProductId": 167343427096,
"UnitPrice": 1.3503340000000000000,
"UnitPriceExcTax": 1.3500343000000000000,
"CostPrice": 0.0000000034300000000,
}
]
},

]

Any Ideas how i can do this?

I have the same question (0)
  • Suggested answer
    Zaid Tariq Profile Picture
    2,274 on at

    Hi,

    You can try Newtonsoft Json parser to parse json objects in your code

  • Charan_12 Profile Picture
    115 on at

    what is Newtonsoft?

  • Dan Kinsella Profile Picture
    468 on at

    Something like this?:

        procedure parseArray(jsonText: Text)
        var
            SalesOrderArray: JsonArray;
            SalesLineArray: JsonArray;
            SalesOrderToken: JsonToken;
            SalesOrderLineToken: JsonToken;
            JsonContents: JsonObject;
        begin
            JsonContents.ReadFrom(jsonText);
            SalesOrderArray := JsonContents.AsToken().AsArray();
            foreach SalesOrderToken in SalesOrderArray do begin
                SalesOrderToken.AsObject().Get('TransactionItems', SalesOrderLineToken);
                SalesLineArray := SalesOrderLineToken.AsArray();
            end;
        end;
  • Verified answer
    Dan Kinsella Profile Picture
    468 on at
    [quote user="Charan_12"]

    what is Newtonsoft?

    [/quote]

    This is a .NET library we use to work with JSON in C/AL.

    With AL we have inbuilt JSON types.

  • Charan_12 Profile Picture
    115 on at

    thanks it worked

  • Terrance Morgan Profile Picture
    515 on at

    You're getting undefined because json.total[0] is itself, an array. You need to isolate the specific array inside json.total[0]. So you would need to do something like json.total[0][0].votes or json.total[0][1].votes.

    I think a better structure for your JSON would be something like this:

    {"total": [

    {

    "votes": "79,060"

    "percent": "11%"

    "winner": "0",

    },

    ...

    {

    "votes": "156,787",

    "percent": "21%",

    "winner": "0"

    }],

    "useWinnerColors": 1,

    "timestamp": "3:00 p.m. April 26",

    "candidateCount": 5

    }

    Now you can do json.total[0].votes.

    You don't need to create an array where each entry is a name-value pair. You can directly use an associative-array.

    EDIT: To iterate over your associative array, use for..in along with hasOwnProperty(). The hasOwnProperty() check will prevent you from iterating over properties that it has inherited (some third-party libraries pollute the namespace):

    var map = json.total[0];

    for(var prop in map) if(map.hasOwnProperty(prop)) {

    var value = map[prop];

    ...

    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,229

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,867 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,153 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans