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

Asked By: Chakra

||

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))
Answered By: Rakesh

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)

Answered By: Sina
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.