0

I am using the below code to parse JSON data to CSV from rest API, but there are some nested objects which I need in different columns, I have tried using JSON normalize but couldn't get through. can anyone please advise?

import requests
import json, pandas as pd
import csv
import urllib

URL = "https://dsv.ihsmvals.com/holidays/v1.0/symbol/FX-GBP/startDate/20210101/endDate/20211231"
response = urllib.request.urlopen(URL)

print(response)
text = response.read()
json_data = json.loads(text)

print(json_data)
df = pd.read_json(URL)
bn = pd.DataFrame(df.events.values.tolist())
pd.json_normalize(bn).head()
bn.to_csv("output.csv")

This is how the output looks enter image description here

This is how it should be enter image description here

Francisco Puga
  • 23,869
  • 5
  • 48
  • 64
Naina
  • 127
  • 9

1 Answers1

0

You probably need to specify the record_path:

bn = pd.json_normalize(bn)
bn.join(pd.json_normalize(bn.pop('events'))).head()
U13-Forward
  • 69,221
  • 14
  • 89
  • 114