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.