Search on blog:

Python: How to read incorrect/dirty JSON data?

If you have incorrect (dirty) JSON data then you can try to use module dirtyjson

For example it can read - dictionary keys without " " (but not dictionary values), - dictionary keys/values with ' ' instead of " ", - dictionary/list elements with , after last element

It can't read empty data.

Example which show it

import dirtyjson

data_string = """
{
    "place1":{
        region: "Europe",
        name: "Paris"
    },
    "place2" : {
        region: 'Europe',
        name: 'Berlin',
    }
}
"""

print('--- OK ----')

data = dirtyjson.loads(data_string)
print(data)
print(data['place1'])
print(data['place2'])

print('--- ERROR ----')

data = dirtyjson.loads('')
print(data)

Instalation

$ pip install dirtyjson

More in doc: dirtyjson

« Page: 1 / 1 »