I am trying to convert a complex json (in nested format) to csv
.
{
"caudal": [
{"ts": 1612746051248, "value": "0.0"},
{"ts": 1612745450856, "value": "0.0"},
{"ts": 1612744250898, "value": "0.0"},
{"ts": 1612743650861, "value": "0.0"},
{"ts": 1612743050821, "value": "0.0"}
],
"FreeHeap": [
{"ts": 1612746051248, "value": "247564"},
{"ts": 1612745450856, "value": "247564"},
{"ts": 1612744250898, "value": "247564"},
{"ts": 1612743650861, "value": "247564"},
{"ts": 1612743050821, "value": "247564"}
],
"MinimoFreeHeap": [
{"ts": 1612746051248, "value": "237440"},
{"ts": 1612745450856, "value": "237440"},
{"ts": 1612744250898, "value": "237440"},
{"ts": 1612743650861, "value": "237440"},
{"ts": 1612743050821, "value": "237440"}
]
}
The jsons that my program must process contain many more records, but I made it smaller to simplify the analysis.I have tried using pandas library as follows:
import pandas as pd
with open('read.json') as f_input:
df = pd.read_json(f_input)
df.to_csv('out.csv', encoding='utf-8', index=False)
And I get the following result:
caudal,FreeHeap,MinimoFreeHeap
"{'ts': 1612746051248, 'value': '0.0'}","{'ts': 1612746051248, 'value': '247564'}","{'ts': 1612746051248, 'value': '237440'}"
"{'ts': 1612745450856, 'value': '0.0'}","{'ts': 1612745450856, 'value': '247564'}","{'ts': 1612745450856, 'value': '237440'}"
"{'ts': 1612744250898, 'value': '0.0'}","{'ts': 1612744250898, 'value': '247564'}","{'ts': 1612744250898, 'value': '237440'}"
"{'ts': 1612743650861, 'value': '0.0'}","{'ts': 1612743650861, 'value': '247564'}","{'ts': 1612743650861, 'value': '237440'}"
"{'ts': 1612743050821, 'value': '0.0'}","{'ts': 1612743050821, 'value': '247564'}","{'ts': 1612743050821, 'value': '237440'}"
As you can see, the information is each cell is for example:
"{'ts': 1612743050821, 'value': '247564'}"
Which I understand is another Json.. Is there any simple way to add a column named timestamp (ts
) and only put the values in the cells where this json is now?
I believe this would be the correct way, my goal is to transform the information contained in the json into csv format to make it more accessible to be used by third parties (databases or artificial intelligence algorithms). But if you can think of another way or format that is more convenient, I am open to changing my initial idea. I have to admit that I am new to this world.
I thought about going through the json and doing the conversion manually, but it becomes difficult to relate the measurements that have the same timestamp.