-1

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

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Chakra
  • 647
  • 1
  • 8
  • 16

2 Answers2

2

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))
Rakesh
  • 81,458
  • 17
  • 76
  • 113
1

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)

Sina
  • 1,055
  • 11
  • 24