Skip to content

Instantly share code, notes, and snippets.

@codarrenvelvindron
Created March 25, 2021 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codarrenvelvindron/82533ac33721260182749f25ba92f126 to your computer and use it in GitHub Desktop.
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
#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)
@codarrenvelvindron
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment