1

I have a (not valid) json file, which is in UTF-8 format

The rough sketch of the json is:

{u'key': {u'key2': u'value'}, ...., u'key3' : u'value'}

Doing a simple python json.loads() results in the following error :

ValueError: Expecting property name: line 1 column 2 (char 1)

Following some related answers on SO, I tried chanding it to unicode:

line = unicode(line,'utf-8')
data = json.loads(line)


ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

One solution I can think is to replace all single quotes by double quotes and proceed, but I was thinking - if there's an easier solution to parse the file to get python dict?

Ashwini Khare
  • 1,645
  • 6
  • 20
  • 37
  • 1
    I would suggest you run your JSON data through a [JSON Validator](http://jsonformatter.curiousconcept.com/) There's other kind you can google as well. – Leb Jun 25 '15 at 18:39
  • check if there are \r\n present in the source-string and ans by @joe-cheng [refer](http://stackoverflow.com/a/29827074/4161807) – Joshua Baboo Feb 06 '17 at 17:34

1 Answers1

7

This is NOT JSON! It looks like a python serialization through repr which you can load with ast.literal_eval(node_or_string) from the ast-module.

deets
  • 6,285
  • 29
  • 28