Is Google data source JSON not valid?
Asked Answered
Q

1

5

I am implementing a Google data source using their Python library. I would like the response from the library to be able to be imported in another Python script using the simplejson library.

However, even their example doesn't validate in JSONLint:

{cols:
    [{id:'name',label:'Name',type:'string'},
     {id:'salary',label:'Salary',type:'number'},
     {id:'full_time',label:'Full Time Employee',type:'boolean'}],
rows:
    [{c:[{v:'Jim'},{v:800,f:'$800'},{v:false}]},
     {c:[{v:'Bob'},{v:7000,f:'$7,000'},{v:true}]},
     {c:[{v:'Mike'},{v:10000,f:'$10,000'},{v:true}]},
     {c:[{v:'Alice'},{v:12500,f:'$12,500'},{v:true}]}]}

How do I tweak the simplejson 'loads' function to import the above JSON content? I think the main problem is that the object keys are not strings.

I would rather not write a regular expression to convert the keys to strings since I think such code would be annoying to maintain.

I am currently getting an "Expecting property name: line 1 column 1 (char 1)" error when trying to import the above JSON into Python with simplejson.

Quittor answered 10/3, 2009 at 0:7 Comment(0)
W
8

It is considered to be invalid JSON without the string keys.

{id:'name',label:'Name',type:'string'}

must be:

{'id':'name','label':'Name','type':'string'}

According to the Google Data Source page, they're returning invalid JSON. They don't specifically say it, but all their examples lack quotes on the keys.

Here is a fairly complete list of JSON processors for Python which goes into detail about what formats they support, and how well. Most don't support non-string keys, but it appears that demjson will convert it.

easy_install demjson
Weever answered 10/3, 2009 at 1:35 Comment(2)
JSON expects strings as keys, not python dictionaries e.g., {1:1, 2:4} is a valid python dictionary but it is an invalid JSON. {id:1} and {"id":1} is a valid Javascript.Trossachs
To be clear, I meant that the example shown is not a valid python dictionary. Index numbers are legal, but any names must be strings. {id:1} is valid Javascript but invalid Python.Weever

© 2022 - 2024 — McMap. All rights reserved.