0

My code :

import json

a='''
{

     "abc" : "2",
     "phone" : {
      "ss" : "22"
      },
     "def" : "5" ,
     
}'''

info = json.loads(a)

print("Number", info["abc"])

It is giving me this error:

"JSONDecodeError: Expecting property name enclosed in double quotes"

I tried removing the comma but then it gives me the error

"JSONDecodeError: Expecting ',' delimiter"
Jay shah
  • 1
  • 1
  • It's not legal to have a trailing `,` on the end. Having that comma means that the parser expects there to be another key later. – Charles Duffy Aug 07 '20 at 18:14
  • That string isn't valid JSON because of the comma at the end of the key/value list. Unlike python, JSON abhores trailing commas. – tdelaney Aug 07 '20 at 18:15
  • Thanks but i saw a similar code in my python course and he has used comma at the end but it works fine for him – Jay shah Aug 07 '20 at 18:19
  • @tdelaney I have edited the question can you please help me – Jay shah Aug 07 '20 at 18:32
  • You have the same problem with a trailing comma. That is valid python (`ast.literal_eval(a)` would work) but not JSON. Remember that JSON is usually computer generated and its strict about format on purpose. JSON serializers need to be sticklers for details and that means that humans typing JSON need to be too. – tdelaney Aug 07 '20 at 18:51
  • @Jayshah, compare to https://ideone.com/0wIeNv, which removes only the specific comma that shouldn't be there, and works fine. – Charles Duffy Aug 07 '20 at 19:16

0 Answers0