Python JSON.Loads Fails with 'Valueerror: Invalid Control Character At: Line 1 Column 33 (Char 33)'

Python json.loads fails with `ValueError: Invalid control character at: line 1 column 33 (char 33)`

The problem is your unicode string contains carriage returns (\r) and newlines (\n) within a string literal in the JSON data. If they were meant to be part of the string itself, they should be escaped appropriately. If they weren't meant to be part of the string, they shouldn't be in your JSON either.

If you can't fix where you got this JSON string to produce valid JSON, you could either remove the offending characters:

>>> json.loads(s.replace('\r\n', ''))

or escape them manually:

>>> json.loads(s.replace('\r\n', '\\r\\n'))

Invalid control character with Python json.loads

There is no error in your json text.

You can get the error if you copy-paste the string into your Python source code as a string literal. In that case \n is interpreted as a single character (newline). You can fix it by using raw-string literals instead (r'', Use triple-quotes r'''..''' to avoid escaping "' quotes inside the string literal).

Can't fix JSONDecodeError: Invalid control character

Don't generate JSON by hand; let json.dumps do it for you. This will ensure that the newlines appearing in the private key are properly encoded.

config = {
"type": "abc",
"id": "t1-2-3",
"private_key_id": "123",
"private_key": "-----BEGIN PRIVATE KEY----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwkO/V+WyyrmTFVFoDN9sN1+AL+KX/IB3y\nZqLJVPGCdQ1l+TlZXbFKFYMwo/Ca4N8g4sy7ZH/9UcqKTxawyqrh7dPcRWWI\nyvAWaEJVCtZhw+Hp4fkmWdoi\n-----END PRIVATE"
}

dbutils.fs.put("test.json", json.dumps(config))

with open("test.json") as f:
config2 = json.load(f)

Python json.loads shows ValueError: Extra data

As you can see in the following example, json.loads (and json.load) does not decode multiple json object.

>>> json.loads('{}')
{}
>>> json.loads('{}{}') # == json.loads(json.dumps({}) + json.dumps({}))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\json\__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 368, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 1 column 3 - line 1 column 5 (char 2 - 4)

If you want to dump multiple dictionaries, wrap them in a list, dump the list (instead of dumping dictionaries multiple times)

>>> dict1 = {}
>>> dict2 = {}
>>> json.dumps([dict1, dict2])
'[{}, {}]'
>>> json.loads(json.dumps([dict1, dict2]))
[{}, {}]

Python/Json:Expecting property name enclosed in double quotes

This:

{
'http://example.org/about': {
'http://purl.org/dc/terms/title': [
{'type': 'literal', 'value': "Anna's Homepage"}
]
}
}

is not JSON.

This:

{
"http://example.org/about": {
"http://purl.org/dc/terms/title": [
{"type": "literal", "value": "Anna's Homepage"}
]
}
}

is JSON.

EDIT:

Some commenters suggested that the above is not enough.

JSON specification - RFC7159 states that a string begins and ends with quotation mark. That is ".

Single quoute ' has no semantic meaning in JSON and is allowed only inside a string.



Related Topics



Leave a reply



Submit