I get response as text:
import requests as req
url = 'https://api.coingecko.com/api/v3/derivatives'
resp = req.get(url)
print(resp.text) # Printing response
I want variable as array with same data. Like an array = resp.text And then i can: array[0]['market'] and get Deepcoint(Deriatives) output
CodePudding user response:
json will resolve your problem
import requests as req
import json
url = 'https://api.coingecko.com/api/v3/derivatives'
json.loads
will parse the data into list of dictionaries
resp = json.loads(req.get(url).text)
Now you can access the list of dictionary according to your need
print(resp[0]['market'])
will give you 'Deepcoin (Derivatives)'
CodePudding user response:
to parse the response text as a JSON object, you can use the json() method of the Response object returned by the get() function.
import requests as req
# Send GET request to URL
url = 'https://api.coingecko.com/api/v3/derivatives'
response = req.get(url)
# Check status code of response
if response.status_code == 200:
# Parse response as JSON
data = response.json()
# Access data as array
print(data[0]['market'])
else:
print('Error: Could not get data from API')
This will send a GET request to the specified URL and print the value of the market field for the first element in the response array.
Note that the data variable will be a Python dictionary, not a true array. However, you can access the data using array-like notation (e.g. data[0]) if the structure of the data is suitable for this.
I hope this helps!