CSV file to be converted into multiple nested JSON - files/transaction.csv
field1,field2,field3,field4,type
transaction,,,,record
,transaction_details,,,record
,,details,,record
,,,id,string
,,,name,String
,,,address,String
,transaction_date,,,date
,transaction_amt,,,double
balance,,,,record
,amount,,,string
,details,,,record
,,id,,string
,,name,,String
,,address,,String
Python code to generate Json -
import csv
import json
from collections import defaultdict
with open("files/transaction.csv", mode='r') as file:
reader = csv.DictReader(file)
json_data = []
level1 = ''
level2 = ''
level3 = ''
index1 = -1
index2 = -1
index3 = -1
for row in reader:
if row["field1"] != '' and row['type'] == 'record':
json_data.append({
"name": row["field1"],
"type": row["type"],
"field":[]
})
index1 = index1 + 1
if row["field2"] != '':
if row['type'] == 'record':
json_data[index1]["field"].append({
"name": row["field2"],
"type": row["type"],
"field":[]
})
json_data_level1 = json_data[index1]["field"];
index2 = index2 + 1
else:
json_data[index1]["field"].append({
"name": row["field2"],
"type": row["type"]
})
if row["field3"] != '':
json_data_level2 = json_data_level1[index2]["field"];
if row['type'] == 'record':
json_data_level2.append({
"name": row["field3"],
"type": row["type"],
"field":[]
})
index3 = index3 + 1
json_data_level3 = json_data_level2[0]["field"];
else:
json_data_level2.append({
"name": row["field3"],
"type": row["type"]
})
if row["field4"] != '' and row['type'] != 'record':
json_data_level3.append({
"name": row["field4"],
"type": row["type"]
})
#
# with open("files/transaction_output.csv", mode='w') as json_output:
json_output = json.dumps(json_data, indent=4)
print(json_output)
# Iterate over each row
import csv
import json
from collections import defaultdict
with open("files/transaction.csv", mode='r') as file:
reader = csv.DictReader(file)
json_data = []
level1 = ''
level2 = ''
level3 = ''
index1 = -1
index2 = -1
index3 = -1
for row in reader:
if row["field1"] != '' and row['type'] == 'record':
json_data.append({
"name": row["field1"],
"type": row["type"],
"field":[]
})
index1 = index1 + 1
if row["field2"] != '':
if row['type'] == 'record':
json_data[index1]["field"].append({
"name": row["field2"],
"type": row["type"],
"field":[]
})
json_data_level1 = json_data[index1]["field"];
index2 = index2 + 1
else:
json_data[index1]["field"].append({
"name": row["field2"],
"type": row["type"]
})
if row["field3"] != '':
json_data_level2 = json_data_level1[index2]["field"];
if row['type'] == 'record':
json_data_level2.append({
"name": row["field3"],
"type": row["type"],
"field":[]
})
index3 = index3 + 1
json_data_level3 = json_data_level2[0]["field"];
else:
json_data_level2.append({
"name": row["field3"],
"type": row["type"]
})
if row["field4"] != '' and row['type'] != 'record':
json_data_level3.append({
"name": row["field4"],
"type": row["type"]
})
#
# with open("files/transaction_output.csv", mode='w') as json_output:
json_output = json.dumps(json_data, indent=4)
print(json_output)
# Iterate over each row
Output Json -
[
{
"name": "transaction",
"type": "record",
"field": [
{
"name": "transaction_details",
"type": "record",
"field": [
{
"name": "details",
"type": "record",
"field": [
{
"name": "id",
"type": "string"
},
{
"name": "name",
"type": "String"
},
{
"name": "address",
"type": "String"
}
]
}
]
},
{
"name": "transaction_date",
"type": "date"
},
{
"name": "transaction_amt",
"type": "double"
}
]
},
{
"name": "balance",
"type": "record",
"field": [
{
"name": "amount",
"type": "string"
},
{
"name": "details",
"type": "record",
"field": [
{
"name": "id",
"type": "string"
},
{
"name": "name",
"type": "String"
},
{
"name": "address",
"type": "String"
}
]
}
]
}
]