OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to correctly assign a python variable to a API key value

  • Thread starter Thread starter Terry
  • Start date Start date
T

Terry

Guest
A partial API request response is:

Code:
{
    "quoteResponse": {
        "error": null,
        "result": [
            {
                "ask": 209.94,
                "askSize": 2,
                "averageAnalystRating": "2.0 - Buy",
                "averageDailyVolume10Day": 119050820,
                "averageDailyVolume3Month": 67794775,
                "bid": 207.01,
                "bidSize": 30,
                "bookValue": 4.837,
                "cryptoTradeable": false,
                        }
        ]
    }
}

Trying to assign "Ask" value to a variable (X).

Base code to form json is:

Code:
response = requests.request("GET", url, headers=headers, params=querystring)
API_Data = response.json()

API_Data = (json.dumps(API_Data, indent=4, sort_keys=True))
print(API_Data)

I have tried two code methods to assign key value for "ask" to a variable (X) as follows:

Method 1:

Code:
for key in API_Data:
    if key == 'ask':
        X = API_Data[key]

print("ask" = ",X)

Method 2:

Code:
for item in API_Data:
   X = (item['quoteResponse']['result']['ask'])

print("ask" = ",X)
<p>A partial API request response is:</p>
<pre><code>{
"quoteResponse": {
"error": null,
"result": [
{
"ask": 209.94,
"askSize": 2,
"averageAnalystRating": "2.0 - Buy",
"averageDailyVolume10Day": 119050820,
"averageDailyVolume3Month": 67794775,
"bid": 207.01,
"bidSize": 30,
"bookValue": 4.837,
"cryptoTradeable": false,
}
]
}
}
</code></pre>
<p>Trying to assign "Ask" value to a variable (X).</p>
<p>Base code to form json is:</p>
<pre><code>response = requests.request("GET", url, headers=headers, params=querystring)
API_Data = response.json()

API_Data = (json.dumps(API_Data, indent=4, sort_keys=True))
print(API_Data)
</code></pre>
<p>I have tried two code methods to assign key value for "ask" to a variable (X) as follows:</p>
<p>Method 1:</p>
<pre><code>for key in API_Data:
if key == 'ask':
X = API_Data[key]

print("ask" = ",X)
</code></pre>
<p>Method 2:</p>
<pre><code>for item in API_Data:
X = (item['quoteResponse']['result']['ask'])

print("ask" = ",X)
</code></pre>
 

Latest posts

A
Replies
0
Views
1
Anthonin Chaboteaux
A
Top