I've been trying to figure out a good way to load JSON objects in Python. I send this json data:
{'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}}
to the backend where it will be received as a string then I used json.loads(data)
to parse it.
But each time I got the same exception :
ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
I googled it but nothing seems to work besides this solution json.loads(json.dumps(data))
which personally seems for me not that efficient since it accept any kind of data even the ones that are not in json format.
Any suggestions will be much appreciated.
This:
is not JSON.
This:
is JSON.
Quite simply, that string is not valid JSON. As the error says, JSON documents need to use double quotes.
You need to fix the source of the data.
as JSON only allows enclosing strings with double quotes you can manipulate the string like this:
>>> str = str.replace("\'", "\"")
This will replace all occurrences of single quote with double quote in the JSON string
str
.You can also use
js-beautify
which is less strict:$ pip install jsbeautifier $ js-beautify file.js
I had similar problem . Two components communicating with each other was using a queue .
First component was not doing json.dumps before putting message to queue. So the JSON string generated by receiving component was in single quotes. This was causing error
Adding json.dumps started creating correctly formatted JSON & solved issue.
JSON strings must use double quotes. The JSON python library enforces this so you are unable to load your string. Your data needs to look like this:
If that's not something you can do, you could use
ast.literal_eval()
instead ofjson.loads()
I've checked your JSON data
in http://jsonlint.com/ and the results were:
modifying it to the following string solve the JSON error: