another question about this topic.
There are several questions and answer in this community about it:
- how to convert json to csv in python
- Convert Json to CSV using Python
- Python convert JSON to CSV
- Convert JSON to CSV with Python 3
- It follows...
But I think in this case, it doesn't work for this hard json:
MY JSON:
{
"took":32,
"timed_out":false,
"_shards":{
"total":4,
"successful":4,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":94,
"relation":"eq"
},
"max_score":"None",
"hits":[
]
},
"aggregations":{
"hostname":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"396",
"doc_count":47,
"process_name":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":16,
"buckets":[
{
"key":"accounts-daemon",
"doc_count":2,
"process_state":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"sleeping",
"doc_count":2,
"process_pid":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":1092,
"doc_count":2,
"process_ppid":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":1,
"doc_count":2,
"process_pgid":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":1092,
"doc_count":2,
"process_cmdline":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"/usr/lib/accountsservice/accounts-daemon",
"doc_count":2,
"process_username":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"root",
"doc_count":2,
"process_cwd":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"/",
"doc_count":2,
"process_cpu_start_time":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":1570456346000,
"key_as_string":"2019-10-07T13:52:26.000Z",
"doc_count":2,
"process_fd_limit_hard":{
"value":4096.0
},
"process_fd_open":{
"value":8.0
},
"process_memory_size":{
"value":281055232.0
},
"process_memory_rss_bytes":{
"value":6168576.0
},
"process_memory_share":{
"value":5464064.0
},
"process_cpu_total_pct":{
"value":0.0005
},
"process_fd_limit_soft":{
"value":1024.0
},
"process_cpu_system_ticks":{
"value":0.0
},
"process_cpu_user_ticks":{
"value":0.0
},
"process_cpu_total_norm_pct":{
"value":0.0005
},
"process_cpu_total_ticks":{
"value":0.0
},
"process_cpu_total_value":{
"value":18775.0
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
},
{
"key":"961",
"doc_count":47,
"process_name":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":16,
"buckets":[
{
"key":"accounts-daemon",
"doc_count":2,
"process_state":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"sleeping",
"doc_count":2,
"process_pid":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":1070,
"doc_count":2,
"process_ppid":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":1,
"doc_count":2,
"process_pgid":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":1070,
"doc_count":2,
"process_cmdline":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"/usr/lib/accountsservice/accounts-daemon",
"doc_count":2,
"process_username":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"root",
"doc_count":2,
"process_cwd":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"/",
"doc_count":2,
"process_cpu_start_time":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":1570450883000,
"key_as_string":"2019-10-07T12:21:23.000Z",
"doc_count":2,
"process_fd_limit_hard":{
"value":4096.0
},
"process_fd_open":{
"value":8.0
},
"process_memory_size":{
"value":281153536.0
},
"process_memory_rss_bytes":{
"value":5992448.0
},
"process_memory_share":{
"value":5279744.0
},
"process_cpu_total_pct":{
"value":0.0
},
"process_fd_limit_soft":{
"value":1024.0
},
"process_cpu_system_ticks":{
"value":0.0
},
"process_cpu_user_ticks":{
"value":0.0
},
"process_cpu_total_norm_pct":{
"value":0.0
},
"process_cpu_total_ticks":{
"value":0.0
},
"process_cpu_total_value":{
"value":13410.0
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
}
I have tried almost everything and I reached out to parse it with a horrible multiloop code, but I think there is an easier way to do this, but no post in this community has been aple to help me.
Could you help me to obtain something like this?:
My result (incomplete):
hostname process_name process_state ... process_username process_cwd process_cpu_start_time
0 396 accounts-daemon sleeping ... root / 2019-10-07T13:52:26.000Z
1 396 iscsid sleeping ... root / 2019-10-07T13:52:27.000Z
2 396 iscsid sleeping ... root / 2019-10-07T13:52:28.000Z
3 396 ksoftirqd/0 sleeping ... root / 2019-10-07T13:52:28.000Z
4 396 kworker/0:1 sleeping ... root / 2019-10-07T13:52:28.000Z
.. ... ... ... ... ... ... ...
25 961 polkitd sleeping ... root / 2019-10-07T12:21:31.000Z
26 961 rcu_sched sleeping ... root / 2019-10-07T12:21:31.000Z
27 961 systemd sleeping ... root / 2019-10-07T12:20:24.000Z
28 961 systemd-journal sleeping ... root / 2019-10-07T12:20:38.000Z
29 961 unattended-upgr sleeping ... root / 2019-10-07T12:21:26.000Z
I know it is not easy, so I'll appreciate any help.
Thanks in advance.