MUK
MUK

Reputation: 411

Parsing a nested json and save it in csv

I am parsing json using an api using json_normalize, and I have to save it in a csv file.

I have get the json and saved it in a csv but its a nested json and one key and its attributes is not saving as columns like other columns.

getting below output in weather column

[{'id': 600, 'main': 'Snow', 'description': 'light snow', 'icon': '13d'}]

output image

code..............

import requests

import json

import pandas as pd

from pandas.io.json import json_normalize

data = requests.get("http://api.openweathermap.org/data/2.5/forecast?id=524901&APPID=94070a31e4a96500de718a66f86ca6ba").json()



vardump = json.dumps(data)

varobject = json.loads(vardump)

df = json_normalize(varobject['list'])

df.to_csv('Output3.csv')

I want the output to be as columns in same csv file.

Upvotes: 1

Views: 215

Answers (1)

jezrael
jezrael

Reputation: 862731

Use:

df1 = json_normalize(data['list'], 'weather')
df2 = json_normalize(data['list'])

df = df2.drop('weather', axis=1).join(df1)

Upvotes: 1

Related Questions