0

I have a JSON object that was saved to a file using the repr function:

f = open('file', 'w')
f.write(repr(my_json))
f.close()

The encoded JSON now has leading 'u' characters indicating unicode encoding.

{u'messages': 'messages'}

This is expected Python 2.x behavior, but now I'm trying to figure out how to decode the string stored in that file. Everything I've tried has given me the same error:

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

Help?

9
  • Why did you not write them with json.dumps? Commented Jan 22, 2015 at 0:42
  • I didn't write it. I'm just stuck trying to decode it. Commented Jan 22, 2015 at 0:43
  • 1
    @kid_drew I think Rafael is saying that you should not use f.write(repr(my_json)) to write it to file at the beginning. You should use json.dump instead. Commented Jan 22, 2015 at 0:44
  • I don't have any problem even with reading after an f.write which python version are you using? Commented Jan 22, 2015 at 0:46
  • @skyline75489 - I get that, and I already know about using json.dumps. My question isn't about json.dumps, it's about how to undo an existing screwup. The data can't be regenerated. Commented Jan 22, 2015 at 0:47

1 Answer 1

1

Assuming that the data that got written to the file was json(able), that also means that it's representation should be a python literal. So... You can try to "decode" it using ast.literal_eval.

e.g.

import ast
with open('file', 'r') as datafile:
    literal_data = datafile.read()

json_data_decoded = ast.literal_eval(literal_data)

demo that ast.literal_eval works with the u prefix:

>>> repr(data)
"{u'foo': u'bar'}"
>>> ast.literal_eval(repr(data))
{u'foo': u'bar'}
>>> ast.literal_eval(repr(data))['foo']
u'bar'

Note, there might be a few python versions (3.0, 3.1 and 3.2) where this doesn't work. I don't really have a good workaround for those versions -- hopefully you don't need to support them.

Sign up to request clarification or add additional context in comments.

5 Comments

btw just regular eval works fine for me in 2.7 and 3.4
json_data_decoded is still a string with the leading 'u' characters for me.
Scratch that. I screwed something up. It worked. =)
@user3467349 -- Yeah, eval should work but also opens you up to a world of pain if the data you are evaluating happens to be malicious :-)
Luckily, this was just a 1-off. We pulled a huge amount of data from a third party and accidentally stored it in a file incorrectly. The file was like 100MB, so manually making changes would have been painful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.