1

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.

1 Answer 1

1

Use:

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

df = df2.drop('weather', axis=1).join(df1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.