Python/Json:Expecting property name enclosed in double quotes
Solution 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.
Solution 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
Solution 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.
Solution 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.