Home > Back-end >  Retrieve specific data from JSON file in Python
Retrieve specific data from JSON file in Python

Time:01-02

Hello everyone and happy new year! I'm posting for the first time here since I can't find an answer for my problem, I've been searching for 2 days now and can't figure out what to do and it's driving me crazy... Here's my problem:

I'm using the Steamspy api to download data of the 100 most played games in the last 2 weeks on Steam, I'm able to dump the downloaded data into a .json file (dumped_data.json in this case), and to import it back into my Python code (I can print it). And while I can print the whole thing, it's not really useable as it's 1900 lines of more or less useful data, so I want to be able to print only specific items of the dumped data. In this project, I would like to only print the name, developer, owners and price of the first 10 games on the list. It's my first Python project, and I can't figure out how to do this...

Here the Python code:

import steamspypi
import json

#gets data using the steamspy api
data_request = dict()
data_request["request"] = "top100in2weeks"

#dumps the data into json file
steam_data = steamspypi.download(data_request)
with open("dumped_data.json", "w") as jsonfile:
    json.dump(steam_data, jsonfile, indent=4)

#print values from dumped file
f = open("dumped_data.json")
data= json.load(f)

print(data)

And here's an exemple of the first item in the json file:

{
"570": {
    "appid": 570,
    "name": "Dota 2",
    "developer": "Valve",
    "publisher": "Valve",
    "score_rank": "",
    "positive": 1378093,
    "negative": 267290,
    "userscore": 0,
    "owners": "100,000,000 .. 200,000,000",
    "average_forever": 35763,
    "average_2weeks": 1831,
    "median_forever": 1079,
    "median_2weeks": 1020,
    "price": "0",
    "initialprice": "0",
    "discount": "0",
    "ccu": 553462
},

Thanks in advance to everyone that's willing to help me, it would mean a lot.

CodePudding user response:

The following prints the values you desire from the first 10 games:

for game in list(data.values())[:10]:
    print(game["name"], game["developer"], game["owners"], game["price"])
  • Related