Looping through complex JSON in AL

Hi,

I have a complex set of data that I retrieve via a HTTP request and need to output the JSON response into a fields in a table…

The structure of the data is;

[
{
“total”: null,
“items”: [
{
“key”: “key1”,
“label”: “key1 label”,
“value”: “value of field”
},
{
“key”: “key2”,
“label”: “key2 label”,
“value”: “value of field”
},
{
“key”: “key3”,
“label”: “key3 label”,
“value”: {
{
“value”: “another value”,
“ongoing”: 9999
}
}
}
}
},
{

    "total": null,
    "items": [
        {
        {
            "key": "key1",
            "label": "key1 label",
            "value": "value of field"
        },
        {
            "key": "key2",
            "label": "key2 label",
            "value": "value of field"
        },
		{
            "key": "key3",
            "label": "key3 label",
            "value": {
			{
					"value": "another value",
					"ongoing": 9999
				}
			}
        }

	}
}

]

there could be 1 object or 500+

I need to be able to loop through each array element signified by “total”:null as the start of each element and then output to a table…

Struggling to be able to loop through the JSON I’ve received back via HTTP request…

Any help would be greatly appreciated!!!

Cheers

Steve

Hi @Steve_Simpson,

First, I think there are some errors in this Json file. I think this is the correct version:

[
    {
        "total": null,
        "items": [
            {
                "key": "key1",
                "label": "key1 label",
                "value": "value of field"
            },
            {
                "key": "key2",
                "label": "key2 label",
                "value": "value of field"
            },
            {
                "key": "key3",
                "label": "key3 label",
                "value": {
                    "value": "another value",
                    "ongoing": 9999
                }
            }
        ]
    },
    {
        "total": null,
        "items": [
            {
                "key": "key1",
                "label": "key1 label",
                "value": "value of field"
            },
            {
                "key": "key2",
                "label": "key2 label",
                "value": "value of field"
            },
            {
                "key": "key3",
                "label": "key3 label",
                "value": {
                    "value": "another value",
                    "ongoing": 9999
                }
            }
        ]
    }
]

You need two foreach to process the data:

// read the main Json array
JsonArr.ReadFrom(VarText);

// procees each element of the array:
foreach JsonTkn in JsonArr do begin
  // at this point, in the first iteration, JsonTkn has the first Json Object
  JsonObj := JsonTkn.AsObject();
  JsonObj.Get('total', JsonTkn);
  // do something if total is null
  if JsonTkn.AsValue.IsNull() then begin
    // load the second array
    JsonObj.Get('items', JsonTkn);
    JsonArr2 := JsonTkn.AsArray();
    // process the second array
    foreach JsonTkn in JsonArr2 do begin
        // at this point, in the first iteration, JsonTkn has the first Json Object from 'items'
        JsonObj2 := JsonTkn.AsObject();
        JsonObj2.Get('key', JsonTkn);
        // do something with 'key' value
        JsonObj2.Get('label', JsonTkn);
        // do something with 'label' value
        JsonObj2.Get('value', JsonTkn);
        if JsonTkn.IsValue() then
          // do something when "value" is a value
        else
          // do something when "value" is an object
    end;
  end;
end;

I hope you will find this usefull.