44
votes

I have a following JSON string coming from external input source:

{value: "82363549923gnyh49c9djl239pjm01223", id: 17893}

This is wrong-formatted JSON string ("id" and "value" must be in quotes), but I need to parse it anyway. I have tried simplejson and json-py and seems they could not be set up to parse such strings.

I am running Python 2.5 on Google App engine, so any C-based solutions like python-cjson are not applicable.

Input format could be changed to XML or YAML, in adition to JSON listed above, but I am using JSON within the project and changing format in specific place would not be very good.

Now I've switched to XML and parsing the data successfully, but looking forward to any solution that would allow me to switch back to JSON.

4
I'm a little confused about how you can switch to XML, yet not be in control of the JSON data. It sounds like you have some external source of data, in either XML or JSON formats, but its JSON output is permanently broken as shown and you can't do anything about it so your only option is to select the XML version instead? Or am I missing something? - Peter Hansen
you can parse it as YAML without a change, because it is YAML too - mykhal
Peter, you're right - I have an external source of data which I could control only in one way - by saying I want an input in either JSON, XML or YAML. Nadia, thanks - that's my mistake (and due to I am not very familiar with Stackoverflow's interface at the time). - Serge Tarkovski

4 Answers

57
votes

since YAML (>=1.2) is a superset of JSON, you can do:

>>> import yaml
>>> s = '{value: "82363549923gnyh49c9djl239pjm01223", id: 17893}'
>>> yaml.load(s)
{'id': 17893, 'value': '82363549923gnyh49c9djl239pjm01223'}
23
votes

You can use demjson.

>>> import demjson
>>> demjson.decode('{foo:3}')
{u'foo': 3}
1
votes

You could use a string parser to fix it first, a regex could do it provided that this is as complicated as the JSON will get.

0
votes

Pyparsing includes a JSON parser example, here is the online source. You could modify the definition of memberDef to allow a non-quoted string for the member name, and then you could use this to parser your not-quite-JSON source text.

[The August, 2008 issue of Python Magazine has a lot more detailed info about this parser. It shows some sample JSON, and code that accesses the parsed results like it was a deserialized object.