How to Deserialize Json Array with Other JSON Format

So I have this Json file:

{
  "Name": "String",
  "Age "String",
  "Date": "9/2/2021",
  "DataArray": [
    {
      "Account Number": "ABC123",
      "Amount": 100,
      "Product Code": "A1",
    },
    {
        "Account Number": "GHC843",
      "Amount": 250,
      "Product Code": "B3",
    }
  ]
}

I can deserialize and get the value from the keys of the ‘Name’ ‘Age’ and ‘Date’, and now I need to Deserialize JSON Array the ‘DataArray’ but entering the json file would error a
Deserialize JSON Array: Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path '', line 2, position 1.

I know I can manually get each Key and Value by doing:

JsonObj("DataArray")(0)("Key").ToString

But my json file varies and would sometimes contains multiple sets inside the array. I hope there is a way around this.

Hi @Shinjid,

You will need to iterate within the array. Nested Json strings can be challenging but it is easy to parse the structure.

For each i in EnumerableRange(0,JsonObj("DataArray").Count)

Within the loop
JsonObj("DataArray")(i)("Key").ToString

Ensure you set datatype of i as int in the for each properties. This will support n number of items in your DataArray.

Hope this clears your doubt.

1 Like

Thanks. This solve the Iteration. I did a For each ‘item’ in Enumerable.Range(0,JsonObj(“DataArray”).Count). Then I’ll store it on a string collection or array.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.