How to decode invalid json string in python

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

I got the line:

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

This is not a valid JSON string, so I cannot decode it directly using the python API. Python will only accept a JSON string string, for example:

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

where properties are quoted as a string.

+6
source share
4 answers

Use demjson , which has the 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'}} 
+9
source

You can try using a wrapper for the 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 
+3
source

@vartec already pointed out demjson , which works well for slightly invalid JSON. For data that matches JSON even less, I wrote barely_json :

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

prints

 [False, '', {'complete': True, 'where is my value?': ''}] 
+1
source

Not very elegant and not durable (and will break easily), but it may be possible to mix it with something like:

 kludged = re.sub('(?i)([a-z_].*?):', r'"\1":', string) # { "hotel": { "id": "123", "name": "hotel_name"} } 

You may find that using pyparsing and the parsePythonValue.py example could do what you want ... (or easily do this) or jsonParser.py can be changed so as not to require keyword values.

0
source

Source: https://habr.com/ru/post/925836/


All Articles