Home > other >  json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)
json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)

Time:03-10

I am using python 3.9

I get this error:

json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)

when I trying to do:

import json

print(json.loads("['product', 'font', 'graphics', 'photo caption', 'brand', 'advertising', 'technology', 'text', 'graphic design', 'competition']"))

But it works fine when I did:

print(json.loads('["a", "b", "c"]'))

It seems the error is associated with the quotation mark. But may I ask why is that? Thank you!

CodePudding user response:

The short answer is that this is just how the JSON spec works. All strings must be double quoted as you can see from the diagram in the middle of Introducing JSON.

However, you can parse that string in python using ast.literal_eval() rather than json.loads().

import ast
print(ast.literal_eval("['product', 'font', 'graphics', 'photo caption', 'brand', 'advertising', 'technology', 'text', 'graphic design', 'competition']"))
  • Related