It seems to me what is needed (after changing json to action):
file.json
[
[
{
"user": "Phoebe",
"text": "Oh my God, hes lost it. Hes totally lost it.",
"sent": "non-neutral"
},
{
"user": "Monica",
"text": "What?",
"sent": "surprise"
}
],
[{
"user": "Joey",
"text": "Hey Estelle, listen",
"sent": "neutral"
},
{
"user": "Estelle",
"text": "Well! Well! Well! Joey Tribbiani! So you came back huh? They",
"sent": "surprise"
}
]
]
import json
with open('file.json') as data_file:
data = json.load(data_file)
df = pd.concat([pd.DataFrame(x) for x in data], ignore_index=False)
print (df)
sent text user
0 non-neutral Oh my God, hes lost it. Hes totally lost it. Phoebe
1 surprise What? Monica
0 neutral Hey Estelle, listen Joey
1 surprise Well! Well! Well! Joey Tribbiani! So you came ... Estelle
And then:
df.to_csv(file, index=False)
EDIT:
If you want to use a pure python solution without pandas, you can change it a bit with this solution :
import json, csv
with open('file.json') as data_file:
data = json.load(data_file)
f = csv.writer(open("test.csv", "w+"))
f.writerow(["user", "sent", "text"])
for y in data:
for x in y:
f.writerow([x["user"], x["sent"], x["text"]])
source
share