Python/Json:Expecting property name enclosed in double quotes
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Quiet Intelligence
--
Chapters
00:00 Python/Json:Expecting Property Name Enclosed In Double Quotes
00:47 Accepted Answer Score 329
01:23 Answer 2 Score 175
01:58 Answer 3 Score 72
02:23 Answer 4 Score 157
02:33 Thank you
--
Full question
https://stackoverflow.com/questions/3949...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #json #parsing
#avk47
ACCEPTED ANSWER
Score 329
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.
ANSWER 2
Score 175
as JSON only allows enclosing strings with double quotes you can manipulate the string like this:
s = s.replace("\'", "\"")
if your JSON holds escaped single-quotes (\') then you should use the more precise following code:
import re
p = re.compile('(?<!\\\\)\'')
s = p.sub('\"', s)
This will replace all occurrences of single quote with double quote in the JSON string s 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
ANSWER 3
Score 157
import ast
import json
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.
ANSWER 4
Score 72
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.