Trying to parse json in example app

Hi,

I have downloaded the REST api Example on the Xojo website. I have got it all working except for the parsing of the json data that is returned.

The problem is in the following code :

[quote]Dim json() As Auto
json = Xojo.Data.ParseJSON(jsonData)[/quote]

I get the following error message.

[quote]Unhandled TypeMismatchException
Message: Expected array of Auto but got class Xojo.Core.Dictionary[/quote]

I would be grateful for any help with this.

You’re declaring the Json variable as an array. Take off the parentheses.

I tried that and got a different error in the following code

[quote]Dim item As Xojo.Core.Dictionary
Error

Code

For Each a As Xojo.Core.Dictionary In json
CustomerList.AddRow(a.Value(“id”), a.Value(“firstname”), a.Value(“lastname”))
Next[/quote]

I would suggest you try

Dim json As Auto json = Xojo.Data.ParseJSON(jsonData) Break
and look at the value of json in the debugger. That will give you hints as to how to handle the structure of the json string.

[quote]I would suggest you try

Dim json As Auto
json = Xojo.Data.ParseJSON(jsonData)
Break
and look at the value of json in the debugger. That will give you hints as to how to handle the structure of the json string.[/quote]

Thanks for replying but the issue is happening with the following line

json = Xojo.Data.ParseJSON(jsonData)

If you are able to post the json text here (in a code block) that would help. PM me if the data is sensitive.

based on your error the below should work

Dim json As Xojo.Core.Dictionary

json = Xojo.Data.ParseJSON(jsonData)

[quote]based on your error the below should work

Dim json As Xojo.Core.Dictionary

json = Xojo.Data.ParseJSON(jsonData)[/quote]

Hi Tim that was the first thing I tried. And got this error message.

[quote]Unhandled TypeMismatchException
Message: Expected array of class Xojo.Core.Dictionary but got class Xojo.Core.Dictionary[/quote]

What does jsonData look like?

[ --> Array
{ --> Dictionary

Some combination of the above.

Is jsonData a Dictionary? An array of Dictionaries?, An array of Text?

Here is a cutdown version of the json text

{"GetAllCustomers":{"10174":{"FirstName":"Abdul","LastName":"Mcconnell","City":"Colorado Springs","State":"CO","Zip":"80935"},"10179":{"FirstName":"Abel","LastName":"Alexander","City":"Arcadia","State":"IA","Zip":"51430"}}}

So at the highest level, jsonData is a Dictionary. It starts with {

It turns out that it is a Dictionary containing Dictionary values which in turn contain Dictionary values

So I think that the first thing is just to parse the jsonData

Dim firstLevel As Xojo.Core.Dictionary firstLevel = Xojo.Data.ParseJSON(jsonData)

At this point we can forget about JSON. We are done with it. We have “extracted” its contents.

Now we have a Dictionary called firstLevel. It is a slightly complicated Dictionary in that all its Values are Dictionaries.
So lets go look at these entries one at a time. We know that the value of these entries are in fact Dictionaries.

For Each d2 As Xojo.Core.DictionaryEntry In firstLevel Dim secondLevel As Xojo.Core.Dictionary secondLevel = d2.Value Next

But good grief! The secondLevel is a Dictionary and it happens that its Values are Dictionaries as well.
So lets go look at these secondLevel entries one at a time.

For Each d3 As Xojo.Core.DictionaryEntry In secondLevel

Dim thirdLevel As Xojo.Core.Dictionary
thirdLevel = d3.Value

Next

But finally we are getting out of this stack of Dictionaries. thirdLevel is a Dictionary but its values are just text. So we can look at them and put them in a text variable.

valueText = thirdLevel.Value("FirstName") MsgBox(valueText)

So to combine all of this

[code]Dim firstLevel As Xojo.Core.Dictionary
firstLevel = Xojo.Data.ParseJSON(jsonData)

Dim valueText As Text

For Each d2 As Xojo.Core.DictionaryEntry In firstLevel

Dim secondLevel As Xojo.Core.Dictionary
secondLevel = d2.Value

For Each d3 As Xojo.Core.DictionaryEntry In secondLevel

Dim thirdLevel As Xojo.Core.Dictionary
thirdLevel = d3.Value
valueText = thirdLevel.Value("FirstName")

MsgBox(valueText)

Next

Next[/code]

You will see the FirstName Abdul and then Abel.

Working with JSON trips me up every time regardless of whether I’m using Xojo, JavaScript or C#. I had a similar problem but my JSON translated to a Dictionary with Array inside:

{
   "templates":[
      {
         "templateId":"IDCR Allergies List.v0",
         "createdOn":"2017-11-17T15:01:42.253Z"
      },
      {
         "templateId":"IDCR - Laboratory Order.v0",
         "createdOn":"2017-11-17T15:01:42.368Z"
      },
      {
         "templateId":"IDCR - Laboratory Test Report.v0",
         "createdOn":"2017-11-17T15:01:42.293Z"
      },
      {
         "templateId":"IDCR Problem List.v1",
         "createdOn":"2017-11-17T15:01:42.221Z"
      },
      {
         "templateId":"IDCR Procedures List.v0",
         "createdOn":"2017-11-17T15:01:42.562Z"
      },
      {
         "templateId":"Vital Signs Encounter (Composition)",
         "createdOn":"2017-11-17T15:01:42.477Z"
      }
   ]
}

The answer, helpfully given by Kem Tekinay was to extract to a Dictionary then to an array.

// Parse json data into array
Dim json As Xojo.Core.Dictionary = Xojo.Data.ParseJSON(jsonData)
Dim templates() As Auto = json.Value("templates")

Kem posted this little guide too: https://forum.xojo.com/46176-xojo-data-parsejson-confusion/0#p374727.

Robert’s explanation is also really helpful.

Thanks for all your help. With your comments and Waynes help I was not only able to figure it out, but I think I understand json better now.