简体   繁体   中英

Json-like response not recognized by Python

I got a response with urllib3 (also tried requests), decoded it and took in into json.loads,

This is the data after decoding with decode('utf-8') which I want to have as json:

json_thing = b'{\n"stuff": {\n"a": "1",\n"b": "2",\n"d": "3",\n"e": "4",\n"f": "5",\n"g": "",\n"h": "8",\n"i": "9",\n"j": "10",\n"k": "",\n"l": "13",\n"m": "",\n"n": "",\n"o": "",\n"p": [{\n"q":\xc2\xa0"19",\n"r": 1\n}],\n"s": "Jan 1, 2020 1:44 pm",\n"t": "Jan 1, 2020 1:44 pm"\n}\n}'

but I always get and error

raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 18 column 14 (char 482)

When looking on it, after "r": there is only a regular integer and there is at "q" \xc2\xa0, is it possible that I need to clean that out first?

The problem is the non-breaking space character in "q":\xc2\xa0"19",\n .

Replace it after decoding and before deserialising:

>>> json.loads(json_thing.decode('utf-8').replace('\N{NO-BREAK SPACE}', ' '))
{
    'stuff': {
        'a': '1',
        'b': '2',
        'd': '3',
        'e': '4',
        'f': '5',
        'g': '',
        'h': '8',
        'i': '9',
        'j': '10',
        'k': '',
        'l': '13',
        'm': '',
        'n': '',
        'o': '',
        'p': [{'q': '19', 'r': 1}],
        's': 'Jan 1, 2020 1:44 pm',
        't': 'Jan 1, 2020 1:44 pm'
    }
}
>>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM