Created
March 25, 2021 18:57
-
-
Save codarrenvelvindron/82533ac33721260182749f25ba92f126 to your computer and use it in GitHub Desktop.
Example for getting data from json file, made up of data dictionary and list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Convjson, an example for getting data from a json file | |
#25/03/2021 | |
#By Codarren Velvindron | |
#Licence: MIT | |
import json | |
#we init an empty dictionary | |
crypto_dictionary = {} | |
#we read the local .json file | |
filename = 'currency_limits_25032021.json' | |
#we create a function to convert json to dictionary | |
def __convert_to_dict(filename,dictionary): #convert object to dictionary | |
""" from a .json filename, we return a dictionary """ | |
try: | |
with open(filename) as json_file: | |
data = json.load(json_file) | |
dictionary.update(data) | |
except JSONDecodeError: | |
print ('JSON decode failed') | |
#We run our json file through the converter | |
__convert_to_dict(filename,crypto_dictionary) | |
#Our global crypto_dictionary is now updated. | |
#print (crypto_dictionary) | |
data_dict = crypto_dictionary['data'] | |
#print (data) | |
pairs = data_dict['pairs'] | |
#print(pairs) | |
#print(type (pairs)) | |
#Start by checking the size of the pairs list | |
size = len(pairs) | |
print(size) | |
#we now loop through it | |
for i in range(0, size): | |
#print (pairs[i]) | |
crypto = pairs[i]['symbol1'] | |
market = pairs[i]['symbol2'] | |
maxprice = pairs[i]['maxPrice'] | |
if ('BTT' in crypto): #if we want BTT as our data | |
print (crypto,market,maxprice) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sample json file