Nov-27-2019, 09:12 AM
Json file: -
{
"id": 1,
"name": "Auto",
"architecture": {
"axe": [
"j",
"y",
"r",
"t",
"x"
],
"p": [
"Vir",
"M"
]
},
"LL": [
{
"id": 10,
"name": "A",
"xx": "D",
"bb": "aa",
"yy": "S"
]
}
}
python script used:-
{
"id": 1,
"name": "Auto",
"architecture": {
"axe": [
"j",
"y",
"r",
"t",
"x"
],
"p": [
"Vir",
"M"
]
},
"LL": [
{
"id": 10,
"name": "A",
"xx": "D",
"bb": "aa",
"yy": "S"
]
}
}
python script used:-
import sys import json import csv reload(sys) sys.setdefaultencoding('utf8') with open('a.json','r') as f: json_data=json.load(f,strict=False) with open('a.csv','w') as outfile: csv_write=csv.writer(outfile,quoting=csv.QUOTE_ALL) csv_write.writerow(["id","xx"]) for json_d in json_data["a"]: row_array = [] try: row_array.append(json_d["Id"]) except KeyError: row_array.append('') try: row_array.append(json_d["LL"][0]["xx"]) except (KeyError,TypeError): row_array.append('') except TypeError: row_array.append('') csv_write.writerow(row_array) outfile.close()i am getting error -IndexError: list index out of range on command -row_array.append(json_d["LL"][0]["xx"])