I am a total beginner and am currently trying to learn python. Right now objects is my biggest issue. I have a simple project where I request an REST api und the response is json and looks a bit like this:
{
"x": 1,
"y": 2,
"z": [
{"a": 1, "b": 1, "c": 1}
{"a": 3, "b": 4, "c": 5}]
}
Now I need something like this for my csv columns:
for every item in z[]:
a,b,c,x,y
I have several 1000 pages of response. How would you solve this?
Right now I get objects in a single column. Using pandas to write to csv:
data = response.json()
df = pd.DataFrame(data)
df.to_csv("output.csv")
2
Answers
If you just want to write the records in column
z
into the CSV-file:If you want to write the full set of data to the file, you could use
pd.json_normalize
to create the dataframe:For the sample
df
will look like:I interpret your question (like Timus) to mean you want to go from:
to
Even if this is correct, please edit your question and include the expected output CSV.
You can also do this without Pandas, using the csv module.
For every object, loop over the objects in z and write those a, b, and c columns, along with x and y: