JMarcos87
JMarcos87

Reputation: 323

Decoding JSON data leads to "invalid control character" error

import json
people_string = '''
{
    "People": [
        {
            "name": "John Smith",
            "phone": "615-555-7164",
            "emails": ["[email protected]", "[email protected]"],
            "Has license": false
        },
        {
            "name": "John Doe",
            "phone": "560-555-5153",
            "emails": null,
            "Has license: true
        }
    ]
 }
'''
data = json.loads(people_string)

When I try to run this, I get the following errors:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Jorge87\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
  File "C:\Users\Jorge87\AppData\Local\Programs\Python\Python37-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Jorge87\AppData\Local\Programs\Python\Python37-32\lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid control character at: line 14 column 31 (char 361)

I also tried importing simplejson instead, but still doesn´t work.

How can I fix this? What does these error messages mean?

Upvotes: 1

Views: 6304

Answers (3)

alex067
alex067

Reputation: 3281

    {
        "name": "John Doe",
        "phone": "560-555-5153",
        "emails": null,
        "Has license: true
    }

It looks like your Has license key is missing a closing double quotation mark.

Upvotes: 1

Cireo
Cireo

Reputation: 4427

Looking at your error message:

ValueError: Invalid control character at: line 14 column 31 (char 361)

you can see your error is here:

>>> print(people_string.splitlines()[13] + '\n' + ' '*30 + '^')
            "Has license: true
                              ^

Where it sees a newline instead of a ". You forgot a closing double quote for "Has license"

Upvotes: 2

Mehmet Ates
Mehmet Ates

Reputation: 133

You forgot a quotation mark in "Has licence", thats it:

"Has license": true

instead of:

"Has license: true

Upvotes: 1

Related Questions