Error parsing JSON

Hello, I’m new to Xojo, though I’ve been programming in VBA, VB6, VB.Net and C# for 20+ years. I am used to using SOAP webservices but have never used JSON.

For a new project I am working on, I have set up a webservice which uses NewtonSoft to create JSON. The output looks like this in Firefox:

{“array”:[{“ID”:“4”,“CodePurpose”:“OrgType”,“Description”:“Charity”},{“ID”:“1”,“CodePurpose”:“OrgType”,“Description”:“Client”},{“ID”:“2”,“CodePurpose”:“OrgType”,“Description”:“Funder”},{“ID”:“5”,“CodePurpose”:“OrgType”,“Description”:“Housing Association”},{“ID”:“6”,“CodePurpose”:“OrgType”,“Description”:“Landlord”},{“ID”:“3”,“CodePurpose”:“OrgType”,“Description”:“Supplier”},{“ID”:“9”,“CodePurpose”:“PersonType”,“Description”:“Client”},{“ID”:“12”,“CodePurpose”:“PersonType”,“Description”:“Examiner”},{“ID”:“8”,“CodePurpose”:“PersonType”,“Description”:“Manager”},{“ID”:“7”,“CodePurpose”:“PersonType”,“Description”:“Proprietor”},{“ID”:“10”,“CodePurpose”:“PersonType”,“Description”:“Teacher”},{“ID”:“11”,“CodePurpose”:“PersonType”,“Description”:“Trainer”}]}

As I understand it, this is a tagged array with a single dictionary inside it.

I have a Xojo desktop app with a Xojo.Net.HTTPSocket, and a PageReceived event on my main form.
My code on that form is (at the moment):

var jsonData As Text = Xojo.Core.TextEncoding.UTF8.ConvertDataToText(Content)
var jsonArray() As Variant
jsonArray = Xojo.Data.ParseJSON(jsonData)

I am getting a JSON error “Top level JSON objects can only be maps or arrays.”

I have tried the other three methods suggested on the Xojo ParseJSON help page but with no better results.

Can anyone help with this issue? I am happy to share the URL of the webservice if necessary - the data is only test data at the moment.

Thanks
John

The top level of this JSON is an object (Dictionary), not an array. The only key is “array” which contains an array.

This will help visualize it better:

{
  "array": [
    {
      "ID": "4",
      "CodePurpose": "OrgType",
      "Description": "Charity"
    },
    {
      "ID": "1",
      "CodePurpose": "OrgType",
      "Description": "Client"
    },
    {
      "ID": "2",
      "CodePurpose": "OrgType",
      "Description": "Funder"
    },
    {
      "ID": "5",
      "CodePurpose": "OrgType",
      "Description": "Housing Association"
    },
    {
      "ID": "6",
      "CodePurpose": "OrgType",
      "Description": "Landlord"
    },
    {
      "ID": "3",
      "CodePurpose": "OrgType",
      "Description": "Supplier"
    },
    {
      "ID": "9",
      "CodePurpose": "PersonType",
      "Description": "Client"
    },
    {
      "ID": "12",
      "CodePurpose": "PersonType",
      "Description": "Examiner"
    },
    {
      "ID": "8",
      "CodePurpose": "PersonType",
      "Description": "Manager"
    },
    {
      "ID": "7",
      "CodePurpose": "PersonType",
      "Description": "Proprietor"
    },
    {
      "ID": "10",
      "CodePurpose": "PersonType",
      "Description": "Teacher"
    },
    {
      "ID": "11",
      "CodePurpose": "PersonType",
      "Description": "Trainer"
    }
  ]
}

“{}” denotes an object, “[]” is an array.

interesting
tried a copy paste of your json into several online validators
none have rejected it so far
seems legit since the top item can be an object according to the BNF on JSON.org and in ECMA-404
http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf

that said the error I’m seeing is related to jsonArray()

var jsonData As Text = Xojo.Core.TextEncoding.UTF8.ConvertDataToText(Content)
var jsonArray As Auto // <<<<<<<<<<<<
jsonArray = Xojo.Data.ParseJSON(jsonData)

Thanks Kem, and thanks Norman,

I’ve changed the webservice and it now sends this out:

[{“ID”:“4”,“CodePurpose”:“OrgType”,“Description”:“Charity”},{“ID”:“1”,“CodePurpose”:“OrgType”,“Description”:“Client”},{“ID”:“2”,“CodePurpose”:“OrgType”,“Description”:“Funder”},{“ID”:“5”,“CodePurpose”:“OrgType”,“Description”:“Housing Association”},{“ID”:“6”,“CodePurpose”:“OrgType”,“Description”:“Landlord”},{“ID”:“3”,“CodePurpose”:“OrgType”,“Description”:“Supplier”},{“ID”:“9”,“CodePurpose”:“PersonType”,“Description”:“Client”},{“ID”:“12”,“CodePurpose”:“PersonType”,“Description”:“Examiner”},{“ID”:“8”,“CodePurpose”:“PersonType”,“Description”:“Manager”},{“ID”:“7”,“CodePurpose”:“PersonType”,“Description”:“Proprietor”},{“ID”:“10”,“CodePurpose”:“PersonType”,“Description”:“Teacher”},{“ID”:“11”,“CodePurpose”:“PersonType”,“Description”:“Trainer”}]

(This was by changing the parsed object from a .Net DataSet to a .Net DataTable)

I’ve changed the PageReceived code so it reads:

var jsonData As Text = Xojo.Core.TextEncoding.UTF8.ConvertDataToText(Content)
var jsonArray() As Auto
jsonArray = Xojo.Data.ParseJSON(jsonData)

The only change was the ‘Auto’ type.

The result is the same.: Top level JSON objects can only be maps or arrays.

Any thoughts? The videos I watched on this made it look so simple!

Thanks
John

With the previous code parse Json returned a SINGLE object
You were trying to assign it to an array

If you review what I posted earlier you’ll see I removed the () from “jsonArray” and things work because ParseJson returned a single object that contained an array and other objects

Interesting. The online Xojo documentation shows JSON being parsed to an array…

Sorry, I missed that you’d removed the brackets. Anyway, this gives the same error:

var jsonData As Text = Xojo.Core.TextEncoding.UTF8.ConvertDataToText(Content)
var jsonResult As Auto
jsonResult = Xojo.Data.ParseJSON(jsonData)

Thanks again Norman. I have added a textArea to the form and when I dump the jsonData into that it is surrounded by double quotes. Could that be the problem?

works here
http://great-white-software.com/miscellaneous/ParseJSON.xojo_binary_project.zip

Thanks I’ll have a look

yeah parse json returns a single Auto that may contain an array a dictionary etc

Thanks Norman,

Your app has the same code as mine. They both compile OK, but when I download data from the webservice, I get the error.

I am thinking the problem might be the format of the data received - as I said, it is surrounded by double quotes…

I will keep looking.

I just copied & pasted the data from above into the constant in the app I sent

the actual data has a leading and trailing quote ?
thats weird but not so hard to remove

[quote=494838:@Norman Palardy]the actual data has a leading and trailing quote ?
thats weird but not so hard to remove[/quote]
i’d rather fix the webservice to send a proper json response…
if it is by any chance a dump of a string variable (containing the json), then other quotes might be escaped or who-knows-what…

it certainly is. json never has a quote as the first character. it’s therefore expected to get a json parse error.

Thanks Jürg,

I think you are right.

I found an article about data being serialized twice, and I think that’s what’s happened. Not only has the JSON been encapsulated in quotes, but backslashes have been added inside the JSON.

I will look at the webservice again.

Regards
John