adplus-dvertising

How to get only the first value between brackets in this request Json

Asked 2 months ago
Viewed 27 times

I'm new to python and i successfully extract the 'symbol' and 'ask' value, but in the json, 'ask' has 2 values, i only want to get the first part before the comma. I tried to find the awnser on the forum, but it never match my problem.

here is my code

    exchange2 = requests.get("https://ascendex.com/api/pro/v2/futures/ticker")
    e = exchange2.json()
    exchange2 = json_normalize(e['data'])
    exchange2['symbol'] = exchange2['symbol'].str.replace('-PERP', 'USDT')
    exchange2 = pd.DataFrame(exchange2, columns=['symbol', 'ask'])
    print (exchange2)

the result is

           symbol                    ask
0    SHIBUSDT  [0.00001077, 3996990]
1     VETUSDT      [0.03033, 953000]

My expected result is this

       symbol                    ask
0    SHIBUSDT  0.00001077
1     VETUSDT      0.03033

asked 2 months ago

Correct Answer

The desired data value is list in ask key of json data.So you can get the first value using list slicing method

exchange2 = requests.get("https://ascendex.com/api/pro/v2/futures/ticker")
e = exchange2.json()
# k=json.dumps(e)
# #print(e)
# with open('aj.json', 'w') as f:
#     f.write(k)
exchange=[]
for item in e['data']:
    symbol= item['symbol'].replace('-PERP', 'USDT')
    ask=item['ask'][0] 
    print(ask)
    exchange.append({
        'symbol':symbol,
        'ask':ask})

df = pd.DataFrame(exchange)
print (df)

Output:

     symbol         ask
0    SHIBUSDT  0.00001079
1     VETUSDT     0.03033
2    ALGOUSDT      0.3895
3     FTTUSDT       25.83
4     TRXUSDT     0.08098
5     ADAUSDT       0.558
6    FIDAUSDT      0.4855
7     ETHUSDT      1788.8
8    PORTUSDT   999999999
9     BCHUSDT       187.5
10   ATOMUSDT        9.21
11    XTZUSDT       1.913
12   XPRTUSDT       1.408
13    BNBUSDT       297.3
14    LTCUSDT       62.87
15    SOLUSDT      39.463
16    UNIUSDT        4.98
17    ZILUSDT     0.04978
18   IOSTUSDT      0.0173
19   DOGEUSDT     0.08159
20    EOSUSDT       1.271
21   EGLDUSDT        75.9
22    BTCUSDT       29718
23  BCHSVUSDT       56.53
24    APEUSDT       6.115
25    OMIUSDT    0.001624
26    DOTUSDT        9.33
27  MATICUSDT       0.589
28    AKTUSDT      0.3872
29    XRPUSDT      0.3941
30    FILUSDT        7.35
31   CSPRUSDT       0.036
32    ETCUSDT       21.82
33    WOOUSDT      0.1585
34   LINKUSDT       7.327
35    SRMUSDT       1.035
36    FTMUSDT      0.3484
37    ONEUSDT     0.04104
38    XLMUSDT     0.14468
answered 2 months ago