Parse JSON with Python, because I suck

I suck ass at python, I should get better at it but the lib im using is all in python. I am getting a huge json string in a response, I would like to parse it nicely. I also don’t want to spend an hour reading about parsing json in python (since like I said, suck ass with it, so be patient)

it’s sensitive data so sorry if its cryptic (its not all my data), here is some of it I guess

{u'status': {u'user_timestamp': u'2018-05-07T00:00:52Z', u'num_devices': 2, u'features_version': u'*********', u'latest_message_id': 22541205234, u'set
tings_version': u'1104'}, u'messages': [{u'username': u'************', u'e164_contact_value': u'+1***********', u'contact_type': 2, u'read': True, u'mes
sage': u'https://media.*********.com/?t=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6Imx0YXlsb3I4MjYxOSIsImtleSI6IjAxNDBjYjJhLTQxN2EtMTFlOC05ZmZm
LTI3YTcxNzA1MzEzZF8xNTQwNTA5MTk0N18rMTU2NzIyMTAwNDkud2F2In0.W9OaycLgkA6qRUYjf5XUwqD_ymQGcXcAe36JfDP8dMk&h=0140cb2a-417a-11e8-9fff-27a71705313d_*********_%2B1567***********', u'message_direction': 1, u'conversation_filtering': 

I’m taking all suggestions. I already tried importing json and using json.load but got a gay python error, like this one…

  File "example.py", line 14, in <module>
    json.load(getMsgs)
  File "/usr/lib/python2.7/json/__init__.py", line 287, in load
    return loads(fp.read(),
AttributeError: 'dict' object has no attribute 'read'

it’s not imperative, i’ll figure it out but I thought you guys might be able to steer me somewhere helpful or give me the instant gratification we all want when we’re coding.

if you want to parse that json , simply split it by " : " and use the index that you want.

sorry but I used to live by michael jordan… lol i saw your icon, he was awesome… anyway…

there is like A LOT of json and its a bunch of records, so just splitting it wont work for me (im pretty proficient in tons of langs :stuck_out_tongue:) i really do need like a php version of json_decode to prettify or at least give… idk, the json from this is weird lookin to me

michael was and still great !

by the way , after this problem what i recommend is to use the json library in python you can load a json code and parse it using the library for the full read , visit https://docs.python.org/2/library/json.html

You manage json in python by using loads and dumps.

I’m gonna write a working snip to parse this as soon as I have some time.

first u need to convert it to json format by using json.dumps and then use json.loads

You should also fix the json in your example. It’s incomplete/invalid.

A bit hacky but should work if you aren’t actually using unicode characters:

import json
weird_json = "{u'status': {u'user_timestamp': u'2018-05-07T00:00:52Z', u'num_devices': 2, u'features_version': u'*********', u'latest_message_id': 22541205234, u'settings_version': u'1104'}}" #your json string here
json_object = json.loads(weird_json .replace("'",'"').replace('u"','"')) #replace ' with " and remove the unicode marker
print(json.dumps(json_object , indent=4)) #pretty print

Ah, no wonder the json looked funny, I was wondering why it had the single quote and a ‘u’. Also, is my library messed up or am I not doing it right, I keep getting the loads error

Traceback (most recent call last):
  File "example.py", line 13, in <module>
    msgs = json.loads(TN.getMessages())
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer

also, in python, if the def arguments are already defined do I still need to put it in the call? here is the function def

def getMessages(self, start_message_id = 1, page_size = 30, get_all = 1):

If the function is already defined, you don’t use def in the call.

Now about your loads error, are you sure you’re not trying to load a file object or iterator something that’s not a string? loads expects a plain old string with json in it.

Turns out there was a “genJson” def, soooo yea, I should pay more attention. Thanks all for the help!

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.