Using Python to talk to NIS API - Parsing JSON

Hello All,
I’m playing around with some python code that uses the requests module to pull down NEM account info, as well as ticker data from coinmarketcap.com. I’m trying to figure out how to correctly parse the JSON returned from NIS, but I’m struggling. Basically, I want to grab data points via the API and save them to variables. I’m able to get it working for the coinmarketcap ticker info, but not for the NIS API. I think it is related to NIS JSON having nested content, but I’m not sure. I’m not a programmer and I recently started learning Python and JSON, I know I have a huge learning curve ahead of me, but would appreciate any tips on using Python to parse NIS JSON data.

Below is the code that works with the coinmarketcap.com API:

import requests
import json
url = 'https://api.coinmarketcap.com/v1/ticker/nem/'
r = requests.get(url)
parsed_json = json.loads(r.content.decode(‘utf-8’))
nem_rank = (parsed_json[0][‘rank’])
print(nem_rank)
6

Below is the code I’m using for NIS, which is not working (data has been modified in the example):

import requests
import json
url2 = 'http://162.243.141.107:7890/account/get/forwarded?address=5555555555555555555555555555555555
rr = requests.get(url2)
parsed_json2 = json.loads(rr.content.decode(‘utf-8’))
print(parsed_json2)
{‘meta’: {‘cosignatories’: [], ‘cosignatoryOf’: [], ‘status’: ‘LOCKED’, ‘remoteStatus’: ‘ACTIVE’}, ‘account’: {‘address’: ‘NNNNNNNNNNNNNNNNNNNNNNNNNNN’, ‘harvestedBlocks’: 555, ‘balance’: 55555555555, ‘importance’: 0.000555555555, ‘vestedBalance’: 346346363636, ‘publicKey’: ‘5555555555555555555555555555555555555555555’, ‘label’: None, ‘multisigInfo’: {}}}

print((parsed_json2[1][‘balance’]))
Traceback (most recent call last):
File “”, line 1, in
KeyError: 1

No matter what I try, I get the same error. I know I’m missing something pretty fundamental here, but have no clue what.

Not looking to be spoon fed, but any tips, assitance would be greatly appreciated.

Thanks.

Hello.

Maybe this could help you. :wink:
http://www.json.org/

JSON or Java Script Object Notation is a data tree of key value pairs.
That mean that to access any value from an object you need to query it by correct key.

In your example you are trying to get value using “1” as key which is an invalid key because the returned JSON object in your case does not contain any keys “1” on the first level.

To access the ballance of an account you should query like this:
print(parsed_json2[“account”][“balance”])
or
print(parsed_json2.account.balance)

you could try to pull out data for exercise from this link:

http://162.243.141.107:7890/account/get/forwarded?address=NDKSDZ4TXWZ3OWQ5AL23IBCY7XIV42NZ3EJ6VV5P

I hope you manage.
Have fun :wink:

Thank you very much! I appreciate it!

Again Thanks. I’m able to pull out the balance and print it, but I’m unable to assign the value to a variable:

print(parsed_json2[“account”][“balance”])
38482

mybal = parsed_json2([“account”][“balance”])
Traceback (most recent call last):
File “”, line 1, in
TypeError: list indices must be integers or slices, not str

I tried casting it using the int() function, but that didn’t work. The type() function lists it as an dictionary

type(parsed_json2)
<class ‘dict’>

I apologize for the dumb questions. I know I’m missing some programming basics.

Difficult to really tell without seeing more but there is an important difference in those statements:

print(parsed_json2[“account”][“balance”])

vs

mybal = parsed_json2([“account”][“balance”])

In the first line you’re treating parsed_jason2 as dict, which seems to be correct.
In the second line though, you’re calling it like a function (probably because you just copy pasted). Notice the parenthesis.
So then python tries to evaluate what you’re doing inside the arguments which is trying to access the list [“account”] with a key [“balance”] which can’t work since it’s a list, not a dict.
Long story short this should work:

mybal = parsed_json2[“account”][“balance”]

Thanks. That makes sense. I appreciate the explanation.

Thanks for sharing that type of topic from which I able to pull out the balance and print it. I am fresher and learn python from CETPA