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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
as JSON only allows enclosing strings with double quotes you can manipulate the string like this:
str = str.replace("'", """)
if your JSON holds escaped single-quotes (') then you should use the more precise following code:
import re
p = re.compile('(?<!\\)'')
str = p.sub('"', str)
This will replace all occurrences of single quote with double quote in the JSON string str and in the latter case will not replace escaped single-quotes.
You can also use js-beautify which is less strict:
$ pip install jsbeautifier $ js-beautify file.js
Method 3
In my case, double quotes was not a problem.
Last comma gave me same error message.
{'a':{'b':c,}}
^
To remove this comma, I wrote some simple code.
import json
with open('a.json','r') as f:
s = f.read()
s = s.replace('t','')
s = s.replace('n','')
s = s.replace(',}','}')
s = s.replace(',]',']')
data = json.loads(s)
And this worked for me.
Method 4
import ast
inpt = {'http://example.org/about': {'http://purl.org/dc/terms/title':
[{'type': 'literal', 'value': "Anna's Homepage"}]}}
json_data = ast.literal_eval(json.dumps(inpt))
print(json_data)
this will solve the problem.
Method 5
Solution 1 (Very Risky)
You can simply use python eval function.
parsed_json = eval(your_json)
Solution 2 (No Risk)
You can use ast library which is included in python by default, it also safely evaluate the expression.
import ast parsed_json = ast.literal_eval(your_json)
Method 6
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.
Method 7
I’ve checked your JSON data
{'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}}
in http://jsonlint.com/ and the results were:
Error: Parse error on line 1:
{ 'http://example.org/
--^
Expecting 'STRING', '}', got 'undefined'
modifying it to the following string solve the JSON error:
{
"http://example.org/about": {
"http://purl.org/dc/terms/title": [{
"type": "literal",
"value": "Anna's Homepage"
}]
}
}
Method 8
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:
{"http://example.org/about": {"http://purl.org/dc/terms/title": [{"type": "literal", "value": "Anna's Homepage"}]}}
If that’s not something you can do, you could use ast.literal_eval() instead of json.loads()
Method 9
x = x.replace("'", '"')
j = json.loads(x)
Although this is the correct solution, but it may lead to quite a headache if there a JSON like this –
{'status': 'success', 'data': {'equity': {'enabled': True, 'net': 66706.14510000008, 'available': {'adhoc_margin': 0, 'cash': 1277252.56, 'opening_balance': 1277252.56, 'live_balance': 66706.14510000008, 'collateral': 249823.93, 'intraday_payin': 15000}, 'utilised': {'debits': 1475370.3449, 'exposure': 607729.3129, 'm2m_realised': 0, 'm2m_unrealised': -9033, 'option_premium': 0, 'payout': 0, 'span': 858608.032, 'holding_sales': 0, 'turnover': 0, 'liquid_collateral': 0, 'stock_collateral': 249823.93}}, 'commodity': {'enabled': True, 'net': 0, 'available': {'adhoc_margin': 0, 'cash': 0, 'opening_balance': 0, 'live_balance': 0, 'collateral': 0, 'intraday_payin': 0}, 'utilised': {'debits': 0, 'exposure': 0, 'm2m_realised': 0, 'm2m_unrealised': 0, 'option_premium': 0, 'payout': 0, 'span': 0, 'holding_sales': 0, 'turnover': 0, 'liquid_collateral': 0, 'stock_collateral': 0}}}}
Noticed that “True” value? Use this to make things are double checked for Booleans. This will cover those cases –
x = x.replace("'", '"').replace("True", '"True"').replace("False", '"False"').replace("null", '"null"')
j = json.loads(x)
Also, make sure you do not make
x = json.loads(x)
It has to be another variable.
Method 10
As it clearly says in error, names should be enclosed in double quotes instead of single quotes. The string you pass is just not a valid JSON. It should look like
{"http://example.org/about": {"http://purl.org/dc/terms/title": [{"type": "literal", "value": "Anna's Homepage"}]}}
Method 11
I used this method and managed to get the desired output. my script
x = "{'inner-temperature': 31.73, 'outer-temperature': 28.38, 'keys-value': 0}"
x = x.replace("'", '"')
j = json.loads(x)
print(j['keys-value'])
output
>>> 0
Method 12
with open('input.json','r') as f:
s = f.read()
s = s.replace(''','"')
data = json.loads(s)
This worked perfectly well for me. Thanks.
Method 13
The below code snippet will help to transform data into JSON. All single-quotes should be converted into double-quotes to jsonify the data.
data = {
"http://example.org/about": {
"http://purl.org/dc/terms/title": [{
"type": "literal",
"value": "Anna's Homepage"
}]
}}
parsed_data = data.replace("'", '"')
data_json = json.loads(parsed_data)
Method 14
If you want to convert a json string with single quotes to python dict use ast.literaleval()
>>> import ast
>>> payload = "{'hello': 'world'}"
>>> ast.literal_eval(payload)
{'hello': 'world'}
>>> type(ast.literal_eval(payload))
<class 'dict'>
This will convert the payload to a python dict.
Method 15
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
Expecting property name enclosed in double quotes
Adding json.dumps started creating correctly formatted JSON & solved issue.
Method 16
As the other answers explain well the error occurs because of invalid quote characters passed to the json module.
In my case I continued to get the ValueError even after replacing ' with " in my string. What I finally realized was that some quote-like unicode symbols had found their way into my string:
“ ” ‛ ’ ‘ ` ´ ″ ′
To clean all of these you can just pass your string through a regular expression:
import re
raw_string = '{“key”:“value”}'
parsed_string = re.sub(r"[“|”|‛|’|‘|`|´|″|′|']", '"', my_string)
json_object = json.loads(parsed_string)
Method 17
For anyone who wants a quick-fix, this simply replaces all single quotes with double quotes:
import json
predictions = []
def get_top_k_predictions(predictions_path):
'''load the predictions'''
with open (predictions_path) as json_lines_file:
for line in json_lines_file:
predictions.append(json.loads(line.replace("'", """)))
get_top_k_predictions("/sh/sh-experiments/outputs/john/baseline_1000/test_predictions.jsonl")
Method 18
You can use the json5 package https://pypi.org/project/json5/ instead of json package. This package can deal with single quotes. The decoding function is json5.loads(data) and similar to the json package.
Method 19
If you are having a problem transform the dict to string and with double quote, this can help:
json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
Method 20
The json syntax requires quotation marks for each “key” and “value”. This makes it such a robust data format. In the following example I’m using colors and color as a key:
{"colors":[
{
"color":"red",
"value":"#f00"
},
{
"color":"green",
"value":"#0f0"
},
{
"color":"blue",
"value":"#00f"
},
{
"color":"cyan",
"value":"#0ff"
},
{
"color":"magenta",
"value":"#f0f"
},
{
"color":"yellow",
"value":"#ff0"
},
{
"color":"black",
"value":"#000"
}]}
Method 21
I had the same problem and what I did is to replace the single quotes with the double one, but what was worse is the fact I had the same error when I had a comma for the last attribute of the json object. So I used regex in python to replace it before using the json.loads() function. (Be careful about the s at the end of “loads”)
import re
with open("file.json", 'r') as f:
s = f.read()
correct_format = re.sub(", *n *}", "}", s)
data_json = json.loads(correct_format)
The used regex return each comma followed by a newline and “}”, replacing it just with a “}”.
Method 22
I’ve had this error trying to normalize nested JSON column in Pandas. As noted by @Reihan_amn, replacing all single quotes with double quotes may affect the actual content. Therefore, when getting this error, you should replace only the ' that are where " should be in JSON syntax. You can do it with the following regular expression:
import re
import json
invalid_json = """{'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}}"""
valid_json = re.sub( "(?<={)'|'(?=})|(?<=[)'|'(?=])|'(?=:)|(?<=: )'|'(?=,)|(?<=, )'", """, invalid_json)
print(json.loads(valid_json))
This would suffice if the only problem was that there were single quotes (') in places were double quotes (") should be, in your original misformatted JSON document. But you would still get an error, if there are double quotes somewhere in your document that are also not part of JSON syntax. In this case I would suggest a 4 step-solution:
-
Replace all double quotes that are part of JSON syntax with single quotes (with the regex like above but
'and"swapped). -
Replace all (leftover) double quotes with some special character that does not feature in your document, e.g.
``. You can do it withre.sub(""", "``", x). - Replace all single quotes that are where the double quotes in JSON should be, with double quotes, using the regex given above.
You may now load JSON document and read it into a Pandas DataFrame with pd.json_normalize(df["json_col"].apply(json.loads)).
- If you want, you can replace back all
``(or a special character of your choice) with".
Method 23
I would highly recommend usage of json prettify tools like JSON Prettifier for the same as it helped me fix the error of a trailing comma that I had in the JSON file, which yielded the same error.
Method 24
My problem was that I copy and pasted a JSON snippet and the double quotes that were used were somehow a different, unrecognized double quote character. My file was valid JSON after replacing the double quotes.
Method 25
Its easy for example
import json
my_str = '{"message_type": "fixed_price_difference", "message": "Hello hello"}'
print(type(json.loads(my_str)), json.dumps(my_str))
Output:
<class 'dict'> "{"message_type": "fixed_price_difference", "message": "Hello hello"}"
Syntax is very important for example
Bad syntax and not correct:
my_str = "{'message_type': 'fixed_price_difference', 'message': 'Hello hello"}'
Correct syntax:
my_str = '{"message_type": "fixed_price_difference", "message": "Hello hello"}'
Finally:
States that a string begins and ends with quotation mark.
Method 26
Since your string is a valid JavaScript object, you could use the Js2Py library:
import js2py
content = """x = {'http://example.org/about': {'http://purl.org/dc/terms/title': [{'type': 'literal', 'value': "Anna's Homepage"}]}}"""
content = js2py.eval_js(content)
print(content.to_dict())
Method 27
Use the eval function.
It takes care of the discrepancy between single and double quotes.
Method 28
It is always ideal to use the json.dumps() method.
To get rid of this error, I used the following code
json.dumps(YOUR_DICT_STRING).replace("'", '"')
Method 29
I have run into this problem multiple times when the JSON has been edited by hand.
If someone was to delete something from the file without noticing it can throw the same error.
For instance, If your JSON last “}” is missing it will throw the same error.
So If you edit you file by hand make sure you format it like it is expected by the JSON decoder, otherwise you will run into the same problem.
Hope this helps!
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0