I have a csv file in the following format:
| a | b | c | d | e |
|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 |
| 9 | 8 | 7 | 6 | 5 |
I want to convert this csv file to Nested JSON format, like this:
[{"a": 1,
"Purchase" : {
"b": 2,
"c": 3
"d": 4},
"Sales": {
"d": 4,
"e": 5}},
{"a": 9,
"Purchase" : {
"b": 8,
"c": 7},
"Sales": {
"d": 6,
"e": 5}}]
How can I make this transformation? I can't seem to figure out how to make this transformation in Python. Keep in mind this is only sample table, my real table has multiple columns and thousands on rows, so manual operations are not economical.
Till now I have tried this code:
with open("new_data.csv") as f:
reader = csv.DictReader(f)
for r in reader:
r["purchase"] = {"b": r['b'],
"c": r['c'],
}
Here I am trying unsuccessfully to add another key value pair of my required dictionary, but not successfully. Same thing I would have done with Sales also but this is just sample.