1

I've written a script in python to get different items from json response from a webpage. I've partially got success by collecting the name of different items. However, I wish to get the different price of it. When it comes to parse the corresponding values of each item, I've got stuck. Any help on this will be highly appreciated.

site address: web_link

Script I've tried with:

import requests res = requests.get("replace_with_above_url") for items in res.json(): for name in items: print(name) 

This is how the structure look like:

[{"BTC":{"sellPrice":711500,"buyPrice":711150,"lastTradePrice":711150}},{"XRP":{"sellPrice":76.7,"buyPrice":76.6,"lastTradePrice":76.6}},{"NEO":{"sellPrice":8769,"buyPrice":8651,"lastTradePrice":8769}},{"GAS":{"sellPrice":3140,"buyPrice":3105,"lastTradePrice":3105}},{"ETH":{"sellPrice":63500,"buyPrice":62450.01,"lastTradePrice":63500}},{"XLM":{"sellPrice":30.78,"buyPrice":30.61,"lastTradePrice":30.78}}] 

Output I'm having (only):

BTC XRP NEO 

Output I intend to get:

BTC 711500 711150 711150 XRP 76.7 76.6 76.6 so on --- 
2
  • 2
    try print(name,items[name]['sellPrice'],items[name]['buyPrice'],items[name]['lastTradePrice']) Commented Feb 18, 2018 at 9:29
  • @Yunus Saha, yes, it does solve the problem. Thanks a lot. Commented Feb 18, 2018 at 9:58

2 Answers 2

1

Use this:

r = requests.get('https://bitbns.com/order/getTickerAll') for item in r.json(): for key, value in item.items(): print(key, value['sellPrice'], value['buyPrice'], value['lastTradePrice']) 

Output:

BTC 705000 704000 704000 XRP 72.3 72.29 72.29 NEO 8452 8450 8452 GAS 3060 3024 3024 ETH 61000 60700 60700 XLM 29.8 29.78 29.78 

From the documentation:

When looping through dictionaries, the key and corresponding value can be retrieved at the same time using the items() method.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow!! very nice solution. Thanks @Keyur Potdar for your solution along with the link to the docs. Sometimes, it is really difficult to find the right portion of a documentation when in need. +1 for the link.
1

The problem I feel is that you need to understand that you are trying to print in the second loop. If you print in the first iteration, you can see the following result.

import requests res = requests.get("https://bitbns.com/order/getTickerAll") for items in res.json(): print(items) {'BTC': {'sellPrice': 703500, 'buyPrice': 702000, 'lastTradePrice': 702000}} {'XRP': {'sellPrice': 72.89, 'buyPrice': 72.7, 'lastTradePrice': 72.9}} {'NEO': {'sellPrice': 8480, 'buyPrice': 8400, 'lastTradePrice': 8400}} {'GAS': {'sellPrice': 3000, 'buyPrice': 2990, 'lastTradePrice': 2990}} {'ETH': {'sellPrice': 61499, 'buyPrice': 60800, 'lastTradePrice': 60800}} {'XLM': {'sellPrice': 29.6, 'buyPrice': 29.53, 'lastTradePrice': 29.53}} 

Now you need to iterate through the second object, so you can use the following code.

import requests res = requests.get("https://bitbns.com/order/getTickerAll") for item in res.json(): for key,value in item.items(): print(key, value) for subkey,subvalue in value.items(): print(subkey,subvalue) 

The second and third for loops are coming because of Python's assumption that dictionary by definition has arbitrary number of keys. You can continue to drill down as much as you want or follow the previous answer where you have got the exact result as you want.

1 Comment

Another good solution. I wish I could accept both the answers!! Thanks @Haris Np.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.