Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode an invalid json string in python

I wonder if there is a way to decode a JSON-like string.

I got string:

'{ hotel: { id: "123", name: "hotel_name"} }'

It's not a valid JSON string, so I can't decode it directly with the python API. Python will only accept a stringified JSON string like:

 '{ "hotel": { "id": "123", "name": "hotel_name"} }'

where properties are quoted to be a string.

like image 530
Derrick Zhang Avatar asked Sep 20 '12 10:09

Derrick Zhang


3 Answers

Use demjson module, which has ability to decode in non-strict mode.

In [1]: import demjson
In [2]: demjson.decode('{ hotel: { id: "123", name: "hotel_name"} }')
Out[2]: {u'hotel': {u'id': u'123', u'name': u'hotel_name'}}
like image 186
vartec Avatar answered Nov 20 '22 22:11

vartec


You could try and use a wrapper for a JavaScript engine, like pyv8.

import PyV8
ctx = PyV8.JSContext()
ctx.enter()
# Note that we need to insert an assignment here ('a ='), or syntax error.
js = 'a = ' + '{ hotel: { id: "123", name: "hotel_name"} }'
a = ctx.eval(js)
a.hotel.id
>> '123' # Prints
like image 43
Aesthete Avatar answered Nov 20 '22 23:11

Aesthete


@vartec has already pointed out demjson, which works well for slightly invalid JSON. For data that's even less JSON compliant I've written barely_json:

from barely_json import parse
print(parse('[no, , {complete: yes, where is my value?}]'))

prints

[False, '', {'complete': True, 'where is my value?': ''}]
like image 2
Florian Brucker Avatar answered Nov 20 '22 22:11

Florian Brucker