(Jan-29-2023, 11:02 AM)eyavuz21 Wrote: Thanks so much for your help. I have tried your code above - it gives me an empty dataframe.You mess the json file to a string when doing that,most use
When I try the following code:
json.load(file)
and not file.read()
You post complete raw json data,or working smample of it it's big.
Here a comlete example that help if you look at undertand how it works.
Use eg jsoncrack to see the structure better and that is vaild json file.
game.json
Output:{
"results": [
{
"leagues": [
{
"matches": [
{
"league": "Premier League",
"teams": "Liverpool - Chelsea",
"score": [
"3:0"
]
},
{
"league": "Premier League",
"teams": "Man Utd - Arsenal",
"score": [
"2:1"
]
}
]
},
{
"matches": [
{
"league": "La Liga",
"teams": "Atletico Madrid - Villareal",
"score": [
"0:2"
]
}
]
}
]
}
]
}
I want this result of game.json
as a DataFrame.Output: league teams score
0 Premier League Liverpool - Chelsea [3:0]
1 Premier League Man Utd - Arsenal [2:1]
Then i would use json_normalize
like this in code under.import pandas as pd import json with open('game.json') as file: json_data = json.load(file) df = pd.json_normalize(json_data['results'][0]['leagues'][0], record_path='matches')Look at result.
>>> df league teams score 0 Premier League Liverpool - Chelsea [3:0] 1 Premier League Man Utd - Arsenal [2:1]