0

I'm trying to play around with the Telegram bot API. I wanted to decode the JSON objects into an array but it doesn't matter what I try I get JSON format anyway...

import urllib.request
import json

#Your Authentication token here
token = "auth_token" 
website = "https://api.telegram.org/bot" + token

update = urllib.request.urlopen(website + "/getUpdates").read()
updateArray = json.loads(update.decode("utf-8"))


print (updateArray)

Like I said whenever I run this I get JSON format still does anybody have a clue what I'm doing wrong? Sorry for my ignorance I'm kinda new to coding. Thanks in advance

Also I know that there are multiple threads regarding some json decoding issues but none of them are usefull for me.

Naomi,

Naomi
  • 271
  • 1
  • 3
  • 15

1 Answers1

1

The repr of Python's lists and dicts (which is what you get when you print them) looks very similar to JSON arrays and object literals (especially when the other types are all strings and numbers). The parsing is probably working, you just won't see it by print-ing.

Try running:

print(type(updateArray), updateArray)

If the type is str, you probably did something wrong or the data was served incorrectly. But if it's list or dict, you parsed just fine; you can use it as a list or dict in Python.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Thanks for your quick reply but when I try to print specific variables into an array it gives me an KeyError so it looks like it's not working properly although I do see the when I try: print(type(updateArray), updateArray) – Naomi Jan 14 '16 at 03:11
  • If you got a `KeyError`, it's a `dict`, not a `list`. Try printing `print(updateArray.keys())` and you'll see the actual available keys (without the output being cluttered with the presumably much larger values), you can't just make wild guesses. It parsed exactly the way you wanted, you just need to use it correctly, and if you can't figure that out, you really need to run through a complete Python introductory course/tutorial, or you'll just keep stumbling over very small obstacles. – ShadowRanger Jan 14 '16 at 03:13
  • Yeah I see where you're coming from and I appreciate it. But the problem is I did run through a complete Python tutorial I just struggle with libraries and stuff all the time. – Naomi Jan 14 '16 at 03:16
  • @Naomi: At this point, it has nothing to do with the library. You've got plain old Python built-ins, `dict`, `list`, `str`, `int`, `float`, `bool`, etc. You can (probably) ignore the origin of the data, the fact it was originally JSON, etc.; that's not relevant now. Run this code in an interactive interpreter session, then just play around with the result; figure out what the keys are, what the values are, etc. Python's interactive interpreter is one of its biggest strengths; you don't need to write a full module to try something out. – ShadowRanger Jan 14 '16 at 03:21
  • I did print the available keys and to be honest they don't make much sense to me I get the following keys 'result', 'ok' however when run my code without decoding the json I get object like 'message', 'message_id' etc etc I need those instead of the keys. Sorry if I sound really stupid I just don't know how to explain it well because I'm not experienced enough I'd probably have to take another Python tutorial. – Naomi Jan 14 '16 at 03:26
  • 2
    I'm guessing they wrapped the "real" result in a status object. I'm guessing if you accessed `updateArray['result']` it would have the "real" result you're looking for. – ShadowRanger Jan 14 '16 at 03:28