I want to create a CSV file with the columns 'Duration' and 'Attention' from a nested JSON file that looks like this:
[
{
"Id": {
"Address": "",
"Address": "",
"Name": "Avg"
},
"position": 0,
"sender": [
false
],
"Atten": 0,
"Duration": 5,
"End": 1645,
"EndRaw": 16040.395,
"Index": 0,
"__v": 0
},
{
"Id": {
"local": "",
"Address": "",
"trafficPairName": "Avg"
},
"position": 0,
"sender": [
false
],
"Atten": 1,
"Duration": 5,
"End": 1652,
"EndRaw": 166140.3618,
"Index": 1,
"__v": 0
}
]
I am getting an error on the for loop that says "list indices must be integers or slices, not str". How do I properly parse this JSON file and create the CSV file?
Here is my Python code:
import json
import csv
with open('averages.json') as f:
data = json.load(f)
filename = "data.csv"
with open(filename, "w") as file:
csv_file = csv.writer(file)
csv_file.writerow(["Duration", "Attention"])
for item in data["Id"]:
csv_file.writerow([])