I am trying to convert the a json file into a csv file using the pandas package in Python.
Code being used:
import pandas
json_file = ("/home/joe/Documents/code/facebook/json/message_1.json")
output = pandas.read_json(json_file)
f = open("/home/joe/Documents/code/facebook/csv/test_output.csv", "w+")
f.write(output.to_csv())
Sample json:
{
"messages": [
{
"sender_name": "Joe P",
"timestamp_ms": 1576878720049,
"content": "message 3",
"type": "Generic"
},
{
"sender_name": "Joe P",
"timestamp_ms": 1576878681386,
"content": "message 2",
"type": "Generic"
},
{
"sender_name": "Aimee C",
"timestamp_ms": 1576878665008,
"content": "message 1",
"type": "Generic"
}
]
}
i would like the output csv data to be formatted like this:
sender_name |timestamp_ms |content |type
Joe P |1576878720049 |Message 3 |generic
Joe P |1576878681386 |Message 2 |generic
Aimee C |1576878665008 |Message 1 |generic
However, the output data looks like this (only 2 columns instead of 4):
|messages
0 |{'sender_name': 'Joe P', 'timestamp_ms': 1576878720049, 'content': 'message 3', 'type': 'Generic'}
1 |{'sender_name': 'Joe P', 'timestamp_ms': 1576878681386, 'content': 'message 2', 'type': 'Generic'}
2 |{'sender_name': 'Aimee C', 'timestamp_ms': 1576878665008, 'content': 'message 1', 'type': 'Generic'}
I've read through lots of threads related to parsing JSON data with pandas but i can't quite pin down the solution to this.