0

I have a JSON file and the inside of it looks like this:

"{'reviewId': 'gp:AOqpTOGiJUWB2pk4jpWSuvqeXofM9B4LQQ4Iom1mNeGzvweEriNTiMdmHsxAJ0jaJiK7CbjJ_s7YEWKE2DA_Qzo', 'userName': '\u00c0ine Mongey', 'userImage': 'https://play-lh.googleusercontent.com/a-/AOh14GhUv3c6xHP4kvLSJLaRaydi6o2qxp6yZhaLeL8QmQ', 'content': \"Honestly a great game, it does take a while to get money at first, and they do make it easier to get money by watching ads. I'm glad they don't push it though, and the game is super relaxing and fun!\", 'score': 5, 'thumbsUpCount': 2, 'reviewCreatedVersion': '1.33.0', 'at': datetime.datetime(2021, 4, 23, 8, 20, 34), 'replyContent': None, 'repliedAt': None}"

I am trying to convert this into a dict and then to a pandas DataFrame. I tried this but it will just turn this into a string representation of a dict, not a dict itself:

with open('sampledict.json') as f:
    dictdump = json.loads(f.read())
    print(type(dictdump))

I feel like I am so close now but I can't find out what I miss to get a dict out of this. Any input will be greatly appreciated!

2

1 Answer 1

1

If I get your data format correctly, this will work:

with open('sampledict.json') as f:
    d = json.load(f)
d = eval(d)

# Or this works as well
    d = json.loads(f.read())
d = eval(d)
>>> d.keys()
['userName', 'userImage', 'repliedAt', 'score', 'reviewCreatedVersion', 'at', 'replyContent', 'content', 'reviewId', 'thumbsUpCount']
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! It works! I could swear I tried eval already but maybe I put it in the wrong place
Using eval() like this could potentially be dangerous. Code could be hidden in the file. Use ast.literal_eval() instead or just parse the data as JSON twice.
@KlausD. Thanks for pointing it out! I wasn't considering cases like code could be hidden in the file. Using ast.literal_eval() will be definitely safer.

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.