Home > Net >  How can I iterate through nested dictionary?
How can I iterate through nested dictionary?

Time:08-15

{
   "success": true,
   "time": 1660441201,
   "currency": "RUB",
   "items": {
      "186150629_143865972": {
         "price": "300.00",
         "buy_order": 220,
         "avg_price": "279.405789",
         "popularity_7d": "19",
         "market_hash_name": "CS:GO Case Key",
         "ru_name": "\u041a\u043b\u044e\u0447 \u043e\u0442 \u043a\u0435\u0439\u0441\u0430 CS:GO",
         "ru_rarity": "\u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430",
         "ru_quality": "",
         "text_color": "D2D2D2",
         "bg_color": ""
      },
      "186150630_143865972": {
         "price": "993.06",
         "buy_order": 200.02,
         "avg_price": "573.320000",
         "popularity_7d": "1",
         "market_hash_name": "eSports Key",
         "ru_name": "\u041a\u043b\u044e\u0447 eSports",
         "ru_rarity": "\u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430",
         "ru_quality": "",
         "text_color": "D2D2D2",
         "bg_color": ""
      }
}
}

So that is my dictionary.For example I want to get all values from "price" and "market_hash_name" How should I iterate to get them?

CodePudding user response:

Saving the input dictionary you have defined into the variable input_dict, we can extract this information using simple list comprehenstions, either to extract it as tuples or as dictionaries :)

A step by step extraction can be seen here-

>>> input_dict['items']
{'186150630_143865972': {'popularity_7d': '1', 'bg_color': '', 'text_color': 'D2D2D2', 'ru_name': '\\u041a\\u043b\\u044e\\u0447 eSports', 'avg_price': '573.320000', 'price': '993.06', 'market_hash_name': 'eSports Key', 'buy_order': 200.02, 'ru_rarity': '\\u0431\\u0430\\u0437\\u043e\\u0432\\u043e\\u0433\\u043e \\u043a\\u043b\\u0430\\u0441\\u0441\\u0430', 'ru_quality': ''}, '186150629_143865972': {'popularity_7d': '19', 'bg_color': '', 'text_color': 'D2D2D2', 'ru_name': '\\u041a\\u043b\\u044e\\u0447 \\u043e\\u0442 \\u043a\\u0435\\u0439\\u0441\\u0430 CS:GO', 'avg_price': '279.405789', 'price': '300.00', 'market_hash_name': 'CS:GO Case Key', 'buy_order': 220, 'ru_rarity': '\\u0431\\u0430\\u0437\\u043e\\u0432\\u043e\\u0433\\u043e \\u043a\\u043b\\u0430\\u0441\\u0441\\u0430', 'ru_quality': ''}}
>>> list(input_dict['items'].values())
[{'popularity_7d': '1', 'bg_color': '', 'text_color': 'D2D2D2', 'ru_name': '\\u041a\\u043b\\u044e\\u0447 eSports', 'avg_price': '573.320000', 'price': '993.06', 'market_hash_name': 'eSports Key', 'buy_order': 200.02, 'ru_rarity': '\\u0431\\u0430\\u0437\\u043e\\u0432\\u043e\\u0433\\u043e \\u043a\\u043b\\u0430\\u0441\\u0441\\u0430', 'ru_quality': ''}, {'popularity_7d': '19', 'bg_color': '', 'text_color': 'D2D2D2', 'ru_name': '\\u041a\\u043b\\u044e\\u0447 \\u043e\\u0442 \\u043a\\u0435\\u0439\\u0441\\u0430 CS:GO', 'avg_price': '279.405789', 'price': '300.00', 'market_hash_name': 'CS:GO Case Key', 'buy_order': 220, 'ru_rarity': '\\u0431\\u0430\\u0437\\u043e\\u0432\\u043e\\u0433\\u043e \\u043a\\u043b\\u0430\\u0441\\u0441\\u0430', 'ru_quality': ''}]
>>> [(i['price'], i['market_hash_name']) for i in input_dict['items'].values()]
[('993.06', 'eSports Key'), ('300.00', 'CS:GO Case Key')]
>>> [{'price': i['price'], 'market_hash_name': i['market_hash_name']} for i in input_dict['items'].values()]
[{'price': '993.06', 'market_hash_name': 'eSports Key'}, {'price': '300.00', 'market_hash_name': 'CS:GO Case Key'}]

As you can see, the final two extractions give a tuple and a dictionary result with the information you need.

CodePudding user response:

Convert to dataframe, then just call those 2 columns.

import pandas as pd
import json

data = '''{
   "success": true,
   "time": 1660441201,
   "currency": "RUB",
   "items": {
      "186150629_143865972": {
         "price": "300.00",
         "buy_order": 220,
         "avg_price": "279.405789",
         "popularity_7d": "19",
         "market_hash_name": "CS:GO Case Key",
         "ru_name": "\u041a\u043b\u044e\u0447 \u043e\u0442 \u043a\u0435\u0439\u0441\u0430 CS:GO",
         "ru_rarity": "\u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430",
         "ru_quality": "",
         "text_color": "D2D2D2",
         "bg_color": ""
      },
      "186150630_143865972": {
         "price": "993.06",
         "buy_order": 200.02,
         "avg_price": "573.320000",
         "popularity_7d": "1",
         "market_hash_name": "eSports Key",
         "ru_name": "\u041a\u043b\u044e\u0447 eSports",
         "ru_rarity": "\u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430",
         "ru_quality": "",
         "text_color": "D2D2D2",
         "bg_color": ""
      }
}
}'''






jsonData = json.loads(data)
df = pd.DataFrame(jsonData['items']).T

df = df[['price', 'market_hash_name']]

Output:

print(df)
                      price market_hash_name
186150629_143865972  300.00   CS:GO Case Key
186150630_143865972  993.06      eSports Key
  • Related