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.

2
  • How about to provide your code using pandas? stackoverflow.com/help/minimal-reproducible-example Commented Jan 27, 2021 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? Commented Jan 27, 2021 at 16:42

1 Answer 1

4
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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Mohammadreza .The ** index=False, encoding="utf-8" ** part did the trick for me.

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.