Python Json Error , Expectiing comma delimitter
Question:
I tried many ways but not sure why this error coming. This is small script I am trying on SPYDER.
Please help.
import json
myjson = '''
[
"details":[
{
"MyTable":"NEWTABLE",
"ReferTo":"Test"
},
]
]
'''
data = json.loads(myjson)
### ABOVE LINE IS THROWING ERROR, ---> Expecting ',' delimiter
Answers:
The data structure is incorrect. it should be a list of dict or a direct dict
Ex:
myjson = '''{
"details":[
{
"MyTable":"NEWTABLE",
"ReferTo":"Test"
},
]
}'''
Or
myjson = '''[
{
"details":[
{
"MyTable":"NEWTABLE",
"ReferTo":"Test"
},
]}
]'''
import ast
print(ast.literal_eval(myjson))
Python lists could not recognize key: value
items so as the result while json tries to decode the string it’s expecting ,
after "details"
but it’s getting :
instead and throws JsonDecodeError
.
so either you should use bracers {}
instead of brackets []
:
myjson = '''
{
"details":[
{
"MyTable":"NEWTABLE",
"ReferTo":"Test"
},
]
}
'''
or if you want to use lists you should wrap your inside items into dictionary:
myjson = '''
[
{
"details":[
{
"MyTable":"NEWTABLE",
"ReferTo":"Test"
},
]
}
]
'''
then you can easily use json.loads(myjson)
I tried many ways but not sure why this error coming. This is small script I am trying on SPYDER.
Please help.
import json
myjson = '''
[
"details":[
{
"MyTable":"NEWTABLE",
"ReferTo":"Test"
},
]
]
'''
data = json.loads(myjson)
### ABOVE LINE IS THROWING ERROR, ---> Expecting ',' delimiter
The data structure is incorrect. it should be a list of dict or a direct dict
Ex:
myjson = '''{
"details":[
{
"MyTable":"NEWTABLE",
"ReferTo":"Test"
},
]
}'''
Or
myjson = '''[
{
"details":[
{
"MyTable":"NEWTABLE",
"ReferTo":"Test"
},
]}
]'''
import ast
print(ast.literal_eval(myjson))
Python lists could not recognize key: value
items so as the result while json tries to decode the string it’s expecting ,
after "details"
but it’s getting :
instead and throws JsonDecodeError
.
so either you should use bracers {}
instead of brackets []
:
myjson = '''
{
"details":[
{
"MyTable":"NEWTABLE",
"ReferTo":"Test"
},
]
}
'''
or if you want to use lists you should wrap your inside items into dictionary:
myjson = '''
[
{
"details":[
{
"MyTable":"NEWTABLE",
"ReferTo":"Test"
},
]
}
]
'''
then you can easily use json.loads(myjson)