convert csv to json (nested objects)

2024/11/15 12:59:57

I am new to python, and I am having to convert a csv file to json in following format:

CSV File :

firstname, lastname, email, customerid, dateadded, customerstatus
john, doe, [email protected], 124,26/11/18,active
jane, doe, [email protected], 125,26/11/18,active

JSON format:

{firstname: "John",lastname: "Doe",emailOrPhone: "[email protected]",extraFields: [{name: "customerid",value: "124"},{name: "dateadded",value: "26/11/18"},{name: "dateadded",value: "26/11/18"}]
}, {firstname: "Jane",lastname: "Doe",emailOrPhone: "[email protected]",extraFields: [{name: "customerid",value: "125"},{name: "dateadded",value: "26/11/18"},{name: "dateadded",value: "26/11/18"}]
}current code I am using:
import requests
import json
import time
import csv
import json
import glob
import os
import loggingfor filename in glob.glob('D:\\api\\Extract.csv'):csvfile = os.path.splitext(filename)[0]jsonfile = csvfile + '.json'with open(csvfile+'.csv') as f:reader = csv.DictReader(f)rows = list(reader)with open(jsonfile, 'w') as f:json.dump(rows, f)url = 'api_url'with open("D:\\api\\Extract.json", "r") as read_file:data = json.load(read_file)for item in data:headers = {"Authorization" : "key", "Content-Type" : "application/json"}r = requests.post(url, data= json.dumps(item), headers= headers)logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(message)s',handlers=[logging.FileHandler("D:\\api\\log_file.log"),logging.StreamHandler()])

I can produce parent values in json, but I am not sure how do I get sub-nodes and parse column name as values and iterate through entire file like that. Above code converts csv to simple json objects, I want to achieve nested objects. I am thinking maybe appending would be the solution, but not sure how to pass column as value and corresponding data as value.

Answer

You can use csv.DictReader which gives you access to the column name as you're iterating each row. Then you can build each item as follows:

import json
import csvprimary_fields = ['firstname', 'lastname', 'email']
result = []
with open('mydata.csv') as csv_file:reader = csv.DictReader(csv_file, skipinitialspace=True)for row in reader:d = {k: v for k, v in row.items() if k in primary_fields}d['extraFields'] = [{'name': k, 'value': v} for k, v in row.items() if k not in primary_fields]result.append(d)print(json.dumps(result, indent=2))

Output

[{"firstname": "john","lastname": "doe","email": "[email protected]","extraFields": [{"name": "customerid","value": "124"},{"name": "dateadded","value": "26/11/18"},{"name": "customerstatus","value": "active"}]},{"firstname": "jane","lastname": "doe","email": "[email protected]","extraFields": [{"name": "customerid","value": "125"},{"name": "dateadded","value": "26/11/18"},{"name": "customerstatus","value": "active"}]}
]

If you want to set custom field names in your final json (e.g. emailOrPhone for email), you can always manually set field names for d and set the appropriate value

https://en.xdnf.cn/q/71952.html

Related Q&A

How can I read exactly one response chunk with pythons http.client?

Using http.client in Python 3.3+ (or any other builtin python HTTP client library), how can I read a chunked HTTP response exactly one HTTP chunk at a time?Im extending an existing test fixture (writt…

ValueError: cannot reindex from a duplicate axis in groupby Pandas

My dataframe looks like this:SKU # GRP CATG PRD 0 54995 9404000 4040 99999 1 54999 9404000 4040 99999 2 55037 9404000 4040 1556894 3 55148 9404000 4040 1556894 4 55254 94…

How to calculate class weights of a Pandas DataFrame for Keras?

Im tryingprint(Y) print(Y.shape)class_weights = compute_class_weight(balanced,np.unique(Y),Y) print(class_weights)But this gives me an error:ValueError: classes should include all valid labels that can…

How to change the layout of a Gtk application on fullscreen?

Im developing another image viewer using Python and Gtk and the viewer is currently very simple: it consists of a GtkWindow with a GtkTreeView on the left side displaying the list of my images, and a G…

How to upload multiple file in django admin models

file = models.FileField(upload_to=settings.FILE_PATH)For uploading a file in django models I used the above line. But For uploading multiple file through django admin model what should I do? I found t…

Convert numpy array to list of datetimes

I have a 2D array of dates of the form:[Y Y Y ... ] [M M M ... ] [D D D ... ] [H H H ... ] [M M M ... ] [S S S ... ]So it looks likedata = np.array([[2015, 2015, 2015, 2015, 2015, 2015], # ...[ 1, …

PyQt: how to handle event without inheritance

How can I handle mouse event without a inheritance, the usecase can be described as follows:Suppose that I wanna let the QLabel object to handel MouseMoveEvent, the way in the tutorial often goes in th…

DHT22 Sensor import Adafruit_DHT error

So Ive properly attached DHT22 Humidity Sensor to my BeagleBone Black Rev C. Im running OS Mavericks on my MacBook Pro and I followed the directions provided by Adafruit on how to use my DHT22 The webs…

Whats the purpose of package.egg-info folder?

Im developing a python package foo. My project structure looks like this:. ├── foo │ ├── foo │ │ ├── bar.py │ │ ├── foo.py │ │ ├── __init__.py │ ├── README.md …

Implement Causal CNN in Keras for multivariate time-series prediction

This question is a followup to my previous question here: Multi-feature causal CNN - Keras implementation, however, there are numerous things that are unclear to me that I think it warrants a new quest…