Phil Nguyen
Phil Nguyen

Reputation: 89

Converting data inside a JSON file into Python dict

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!

Upvotes: 0

Views: 89

Answers (1)

Yilun Zhang
Yilun Zhang

Reputation: 9018

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']

Upvotes: 1

Related Questions