0% found this document useful (0 votes)
8 views3 pages

Extract Variables From Json

The document discusses how to extract specific variables (Name, Symbol, CoinName, and ID) from a JSON response and save them to a CSV file using Python. It provides example code for retrieving data from a cryptocurrency API and suggests methods for creating a list or dictionary of the desired values. Additionally, it includes advice on writing the extracted data to a CSV file.

Uploaded by

Pawan kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Extract Variables From Json

The document discusses how to extract specific variables (Name, Symbol, CoinName, and ID) from a JSON response and save them to a CSV file using Python. It provides example code for retrieving data from a cryptocurrency API and suggests methods for creating a list or dictionary of the desired values. Additionally, it includes advice on writing the extracted data to a CSV file.

Uploaded by

Pawan kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python: Extract Variables from JSON - Stack Overflow https://stackoverflow.com/questions/46743831/python-extract-variables-...

Python: Extract Variables from JSON


Asked 3 years ago Active 3 years ago Viewed 471 times

1 of 3 10/29/2020, 9:50 PM
Python: Extract Variables from JSON - Stack Overflow https://stackoverflow.com/questions/46743831/python-extract-variables-...

I would like to extract a number of variables from a JSON-reponse and save it in a csv-file.
The JSON-reponse looks as follows:
-3
{
"Response":"Success",
"Message":"Coin list succesfully returned!",
"BaseImageUrl":"https://www.cryptocompare.com",
"BaseLinkUrl":"https://www.cryptocompare.com",
"DefaultWatchlist":{
"CoinIs":"1182,7605,5038,24854,3807,3808,202330,5324,5031,178978",
"Sponsored":"1182"
},
"Data":{
"USC":{
"Id":"100954",
"Url":"/coins/usc/overview",
"ImageUrl":"/media/1383363/usc.png",
"Name":"USC",
"Symbol":"USC",
"CoinName":"Ultimate Secure Cash",
"FullName":"Ultimate Secure Cash (USC)",
"Algorithm":"SHA256",
"ProofType":"PoS",
"FullyPremined":"0",
"TotalCoinSupply":"200084200",
"PreMinedValue":"N/A",
"TotalCoinsFreeFloat":"N/A",
"SortOrder":"1233",
"Sponsored":false

I would like to get the following variables: Name, Symbol, CoinName and ID

In order to extract all the values of the JSON-response, I use the following code:

def getCoinList():
req = requests.get(‘https://www.cryptocompare.com/api/data/coinlist/’).json()
info = req[‘Data’]
coinList = pd.DataFrame(info)
coinList = coinList.transpose()
coinList.to_csv(‘coinList.csv’)
return coinList

However, I would like to extract only the specified variables.

def getCoinList():
req = requests.get(‘https://www.cryptocompare.com/api/data/coinlist/’).json()['Data']
info = req['...'] /// How do I all four variables?
coinList = pd.DataFrame(info)
coinList = coinList.transpose()
coinList.to_csv(‘coinList.csv’)
return coinList

I am not sure how I can modify this code to extract only the four specified variables? Can
anyone help me with this. Thanks in advance,

2 of 3 10/29/2020, 9:50 PM
Python: Extract Variables from JSON - Stack Overflow https://stackoverflow.com/questions/46743831/python-extract-variables-...

Use a dictionary – Adi219 Oct 14 '17 at 11:09

What Python version supports ‘ and ’ and doesn't need indentation? And your "JSON" isn't JSON,
there are at least a few } missing. – Stefan Pochmann Oct 14 '17 at 11:18

1 what if you open your .json file like , with open (file, 'r') as f:... and after that you manage your
dictionary getting the keys with: yourdict.get('yourkey') .. of course this works if you know by hand
the name of the keys to get. – Nestor Colt Oct 14 '17 at 11:18

1 Answer Active Oldest Votes

If you want a list containing several specific values in your dict , you need to manually create
a list:
1
mylist = [req['Name'], req['Symbol'], req['CoinName'], req['ID']]

Or, if you want a dict :

mydict = {'Name': req['Name'], 'Symbol': req['Symbol'],


'CoinName': req['CoinName'], 'ID': req['ID']}

So to write this to a csv:

import csv

with open('coinList.csv', 'a') as filep:


csvwriter = csv.writer(filep)
csvwriter.writerow([req['Name'], req['Symbol'], req['CoinName'], req['ID']])

answered Oct 14 '17 at 11:31


jpyams
2,749 2 26 56

3 of 3 10/29/2020, 9:50 PM

You might also like