1

I have a JSON response in the form of a JSON object from a request in the pattern:

{"a":[1,2,3,4,5],"b":[I,II,III,IV,V],"c":[p,q,r,s,t]}

How can I parse this json object into a csv file in python containing a,b,c as column names and the values as data in rows as:

a    b      c
1    I      p
2    II     q
3    III    r
4    IV     s
5    V      t

Code for json response:

url="some url"

page=requests.get(url)
output = page.json()

The closest answer I got to was in How can I convert JSON to CSV?

I have tried to convert it into a pandas dataframe and iterate through it but I can't get the work around with it with my JSON object.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • How about to provide your code using `pandas`? https://stackoverflow.com/help/minimal-reproducible-example – ghchoi Jan 27 '21 at 16:41
  • There is an answer in the link you provide which creates a `csv` file. Did you try that? Did you have to make any changes to get it to work for your data? – quamrana Jan 27 '21 at 16:42

1 Answers1

3
pip3 install pandas

Install the Pandas library with the above command.

import pandas as pd

output = {
    "a": [1, 2, 3, 4, 5],
    "b": ["I", "II", "III", "IV", "V"],
    "c": ["p", "q", "r", "s", "t"],
}

df = pd.DataFrame(output)
df.to_csv("filename.csv", index=False, encoding="utf-8")

Output:

enter image description here

Cosmos
  • 372
  • 2
  • 6