Nov-27-2019, 04:24 PM
i have below json file. When i amtrying to pull sid and type from applicationRoles, i am able to pull only first entry i.e.sid=123 and type=BC but i need both entries
{
"applicationgroup":[
{
"name":"test"
"applicationRoles":[
{
"sid":123
"type":'BC'
},
{
"sid":234
"type":'adBC'
}
],
"type":"tttt"
}
]
}
my code: -
{
"applicationgroup":[
{
"name":"test"
"applicationRoles":[
{
"sid":123
"type":'BC'
},
{
"sid":234
"type":'adBC'
}
],
"type":"tttt"
}
]
}
my code: -
import sys import json import csv with open('test.json','r') as f: json_data=json.data(f,strict=false) with open('test.csv','w') as out: csv_write=csv.writer(out) csv_write.writerow(["name","sid","type"]) for json_d in json_data["applicationgroups"]: row_array=[] try: row_array.append(json_d["name"]) except KeyError: row_array.append(") try: row_array.append(json_d["applicationRoles"][0]["sid"]) except (KeyError,TypeError): row_array.append(") try: row_array.append(json_d["applicationRoles"][0]["type"]) except (KeyError,TypeError): row_array.append(") except TypeError: row_array.append(") csv_write.writerow(row_array) out.close()