Bakkar Bakkar
Bakkar Bakkar

Reputation: 64

Print specific value

Here I am trying to get the XLM volume change per second! I got the API but when printing it gives me all the values! Clearly this is not what I really want all I want is the "volume" value!

CODE

import time, base64, hmac, hashlib, requests, json



apiKey = "public api goes here"
apiSecret = "private api goes here"
apiSecret = base64.b64decode(apiSecret)
stamp = str(int(time.time())*1000)
data = "{}{}".format(apiKey, stamp).encode('utf-8')
signature = hmac.new(apiSecret, data, hashlib.sha256).digest()
signature = base64.b64encode(signature)

headers= {
  "X-PCK": apiKey,
  "X-Stamp": str(stamp),
  "X-Signature": str(base64.b64encode(signature).decode('utf-8')),
  "Content-Type": "application/json "}



base = "https://api.btcturk.com"
method = "/api/v2/ticker?pairSymbol=XLM_TRY"
uri = base+method

result = requests.get(url=uri)
result = result.json()
print(json.dumps(result, indent=2))

OUTPUT

{
  "data": [
    {
      "pair": "XLMTRY",
      "pairNormalized": "XLM_TRY",
      "timestamp": 1610020217912,
      "last": 2.45,
      "high": 3.35,
      "low": 2.0,
      "bid": 2.4489,
      "ask": 2.45,
      "open": 2.1786,
      "volume": 551009229.0058,
      "average": 2.4422,
      "daily": 0.2714,
      "dailyPercent": 12.46,
      "numeratorSymbol": "XLM",
      "order": 1000
    }
  ],
  "success": true,
  "message": null,
  "code": 0

WANTED OUTPUT

"volume": 551009229.0058

Upvotes: 0

Views: 52

Answers (1)

quamrana
quamrana

Reputation: 39404

In order to get the "volume" entry you just need to navigate through the structures:

result = requests.get(url=uri)
result = result.json()
print(result["data"][0]["volume"])

There is no need to convert result back to json. The call to request.json() converts the json string returned in the requests.get() to a python data structure, namely a dict with a "data" entry which has a list with one entry in which is another dict where the entry "volume" is located.

Upvotes: 1

Related Questions