Using pyjq i am able to parse values from json file. I need to format the output bit more so this can be exported to csv.
import json
import csv
import pyjq
emp_data = open('example.json', 'r')
emp_data_parsed = json.loads(emp_data.read())
emp = pyjq.all ('.base[].base[].uid, .base[].base[].name', emp_data_parsed)
print emp
The output I am getting
[u'2da21174-0af8-4b5b-b02e-2957a24d70e1', u'fcc5a2c8-3a78-4cc5-9fd3-e7bd59eb36ba', u'4ecf6450-7307-466c-bf19-663ba2fbaf69', None, u'Tommy', u'Sam',
Expecting output as below so that can be written to a csv file.
uid,name
'2da21174-0af8-4b5b-b02e-2957a24d70e1','None'
'fcc5a2c8-3a78-4cc5-9fd3-e7bd59eb36ba','Tommy'
'4ecf6450-7307-466c-bf19-663ba2fbaf69','Sam'
Following is the sample.json file
example.json
{
"base": [
{
"base": [
{
"item-number": 1,
"type": "access-item",
"uid": "2da21174-0af8-4b5b-b02e-2957a24d70e1",
"usage": {
"last-date": {
"iso-8601": "2018-03-19T03:58-0500",
},
},
"item-number": 2,
"name": "Tommy",
"type": "access-item",
"uid": "fcc5a2c8-3a78-4cc5-9fd3-e7bd59eb36ba",
"item-number": 3,
"name": "Sam",
"type": "access-item",
"uid": "4ecf6450-7307-466c-bf19-663ba2fbaf69",
"usage": {
"last-date": {
"iso-8601": "2018-03-21T07:21-0500",
},
},
}
],
}
],
}
I am not sure otherthan pyjq, there is an way of doing this. If so please let me know.