convert csv to json (nested objects)
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, john.doe@do.com, 124,26/11/18,active
jane, doe, jane.doe@do.com, 125,26/11/18,active
JSON format:
{
firstname: "John",
lastname: "Doe",
emailOrPhone: "john.doe@do.com",
extraFields: [{
name: "customerid",
value: "124"
},
{
name: "dateadded",
value: "26/11/18"
},
{
name: "dateadded",
value: "26/11/18"
}
]
}, {
firstname: "Jane",
lastname: "Doe",
emailOrPhone: "Jane.doe@do.com",
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 logging
for 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.
python json
add a comment |
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, john.doe@do.com, 124,26/11/18,active
jane, doe, jane.doe@do.com, 125,26/11/18,active
JSON format:
{
firstname: "John",
lastname: "Doe",
emailOrPhone: "john.doe@do.com",
extraFields: [{
name: "customerid",
value: "124"
},
{
name: "dateadded",
value: "26/11/18"
},
{
name: "dateadded",
value: "26/11/18"
}
]
}, {
firstname: "Jane",
lastname: "Doe",
emailOrPhone: "Jane.doe@do.com",
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 logging
for 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.
python json
Can you share your code so that we can update it from there
– Krishna
Nov 26 '18 at 2:55
Hi I have added my code in the question above. That might not help as I am converting csv to json straight away.
– jdag
Nov 26 '18 at 3:19
Possible duplicate of How to convert CSV to JSON?
– SuperShoot
Nov 26 '18 at 3:20
add a comment |
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, john.doe@do.com, 124,26/11/18,active
jane, doe, jane.doe@do.com, 125,26/11/18,active
JSON format:
{
firstname: "John",
lastname: "Doe",
emailOrPhone: "john.doe@do.com",
extraFields: [{
name: "customerid",
value: "124"
},
{
name: "dateadded",
value: "26/11/18"
},
{
name: "dateadded",
value: "26/11/18"
}
]
}, {
firstname: "Jane",
lastname: "Doe",
emailOrPhone: "Jane.doe@do.com",
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 logging
for 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.
python json
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, john.doe@do.com, 124,26/11/18,active
jane, doe, jane.doe@do.com, 125,26/11/18,active
JSON format:
{
firstname: "John",
lastname: "Doe",
emailOrPhone: "john.doe@do.com",
extraFields: [{
name: "customerid",
value: "124"
},
{
name: "dateadded",
value: "26/11/18"
},
{
name: "dateadded",
value: "26/11/18"
}
]
}, {
firstname: "Jane",
lastname: "Doe",
emailOrPhone: "Jane.doe@do.com",
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 logging
for 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.
python json
python json
edited Nov 26 '18 at 3:49
RoadRunner
11.3k31341
11.3k31341
asked Nov 26 '18 at 2:40
jdagjdag
649
649
Can you share your code so that we can update it from there
– Krishna
Nov 26 '18 at 2:55
Hi I have added my code in the question above. That might not help as I am converting csv to json straight away.
– jdag
Nov 26 '18 at 3:19
Possible duplicate of How to convert CSV to JSON?
– SuperShoot
Nov 26 '18 at 3:20
add a comment |
Can you share your code so that we can update it from there
– Krishna
Nov 26 '18 at 2:55
Hi I have added my code in the question above. That might not help as I am converting csv to json straight away.
– jdag
Nov 26 '18 at 3:19
Possible duplicate of How to convert CSV to JSON?
– SuperShoot
Nov 26 '18 at 3:20
Can you share your code so that we can update it from there
– Krishna
Nov 26 '18 at 2:55
Can you share your code so that we can update it from there
– Krishna
Nov 26 '18 at 2:55
Hi I have added my code in the question above. That might not help as I am converting csv to json straight away.
– jdag
Nov 26 '18 at 3:19
Hi I have added my code in the question above. That might not help as I am converting csv to json straight away.
– jdag
Nov 26 '18 at 3:19
Possible duplicate of How to convert CSV to JSON?
– SuperShoot
Nov 26 '18 at 3:20
Possible duplicate of How to convert CSV to JSON?
– SuperShoot
Nov 26 '18 at 3:20
add a comment |
3 Answers
3
active
oldest
votes
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 csv
primary_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": "john.doe@do.com",
"extraFields": [
{
"name": "customerid",
"value": "124"
},
{
"name": "dateadded",
"value": "26/11/18"
},
{
"name": "customerstatus",
"value": "active"
}
]
},
{
"firstname": "jane",
"lastname": "doe",
"email": "jane.doe@do.com",
"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
add a comment |
At little more complicated than needs to be, but you can try building your JSON array as you read in your values from the csv file, then output your result to a .json
file with json.dump
at the end:
from csv import reader
from json import dump
top_fields = ["firstname", "lastname", "email"]
extra_fields = ["customerid", "dateadded", "customerstatus"]
data =
with open("customers.csv") as csv_in:
csv_reader = reader(csv_in)
# Get headers
headers = list(map(str.strip, next(csv_reader)))
for row in csv_reader:
json_object = {}
# Build dictionary for each row
row_map = dict(zip(headers, map(str.strip, row)))
# Add in top fields first
for top in top_fields:
json_object[top] = row_map[top]
# Then add in extra fields
for extra in extra_fields:
json_object.setdefault("extraFields", ).append(
{"name": extra, "value": row_map[extra]}
)
data.append(json_object)
with open("customers.json", "w") as fp:
dump(data, fp, indent=4, sort_keys=True)
Which gives the following customers.json:
[
{
"email": "john.doe@do.com",
"extraFields": [
{
"name": "customerid",
"value": "124"
},
{
"name": "dateadded",
"value": "26/11/18"
},
{
"name": "customerstatus",
"value": "active"
}
],
"firstname": "john",
"lastname": "doe"
},
{
"email": "jane.doe@do.com",
"extraFields": [
{
"name": "customerid",
"value": "125"
},
{
"name": "dateadded",
"value": "26/11/18"
},
{
"name": "customerstatus",
"value": "active"
}
],
"firstname": "jane",
"lastname": "doe"
}
]
add a comment |
import csv
import sys
import json
#EDIT THIS LIST WITH YOUR REQUIRED JSON KEY NAMES
fieldnames=["firstname","secondname","age"]
def convert(filename):
csv_filename = filename[0]
print "Opening CSV file: ",csv_filename
f=open(csv_filename, 'r')
csv_reader = csv.DictReader(f,fieldnames)
json_filename = csv_filename.split(".")[0]+".json"
print "Saving JSON to file: ",json_filename
jsonf = open(json_filename,'w')
data = json.dumps([r for r in csv_reader])
jsonf.write(data)
f.close()
jsonf.close()
if __name__=="__main__":
convert(sys.argv[1:])
USAGE:
python csv2json.py myCSVfile.txt
where myCSVfile.txt it's your CSV file (name it as you prefer).
It will create a JSON array in a file named myCSVfile.json
That's all.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474118%2fconvert-csv-to-json-nested-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 csv
primary_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": "john.doe@do.com",
"extraFields": [
{
"name": "customerid",
"value": "124"
},
{
"name": "dateadded",
"value": "26/11/18"
},
{
"name": "customerstatus",
"value": "active"
}
]
},
{
"firstname": "jane",
"lastname": "doe",
"email": "jane.doe@do.com",
"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
add a comment |
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 csv
primary_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": "john.doe@do.com",
"extraFields": [
{
"name": "customerid",
"value": "124"
},
{
"name": "dateadded",
"value": "26/11/18"
},
{
"name": "customerstatus",
"value": "active"
}
]
},
{
"firstname": "jane",
"lastname": "doe",
"email": "jane.doe@do.com",
"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
add a comment |
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 csv
primary_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": "john.doe@do.com",
"extraFields": [
{
"name": "customerid",
"value": "124"
},
{
"name": "dateadded",
"value": "26/11/18"
},
{
"name": "customerstatus",
"value": "active"
}
]
},
{
"firstname": "jane",
"lastname": "doe",
"email": "jane.doe@do.com",
"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
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 csv
primary_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": "john.doe@do.com",
"extraFields": [
{
"name": "customerid",
"value": "124"
},
{
"name": "dateadded",
"value": "26/11/18"
},
{
"name": "customerstatus",
"value": "active"
}
]
},
{
"firstname": "jane",
"lastname": "doe",
"email": "jane.doe@do.com",
"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
answered Nov 26 '18 at 3:20
sliderslider
8,47311231
8,47311231
add a comment |
add a comment |
At little more complicated than needs to be, but you can try building your JSON array as you read in your values from the csv file, then output your result to a .json
file with json.dump
at the end:
from csv import reader
from json import dump
top_fields = ["firstname", "lastname", "email"]
extra_fields = ["customerid", "dateadded", "customerstatus"]
data =
with open("customers.csv") as csv_in:
csv_reader = reader(csv_in)
# Get headers
headers = list(map(str.strip, next(csv_reader)))
for row in csv_reader:
json_object = {}
# Build dictionary for each row
row_map = dict(zip(headers, map(str.strip, row)))
# Add in top fields first
for top in top_fields:
json_object[top] = row_map[top]
# Then add in extra fields
for extra in extra_fields:
json_object.setdefault("extraFields", ).append(
{"name": extra, "value": row_map[extra]}
)
data.append(json_object)
with open("customers.json", "w") as fp:
dump(data, fp, indent=4, sort_keys=True)
Which gives the following customers.json:
[
{
"email": "john.doe@do.com",
"extraFields": [
{
"name": "customerid",
"value": "124"
},
{
"name": "dateadded",
"value": "26/11/18"
},
{
"name": "customerstatus",
"value": "active"
}
],
"firstname": "john",
"lastname": "doe"
},
{
"email": "jane.doe@do.com",
"extraFields": [
{
"name": "customerid",
"value": "125"
},
{
"name": "dateadded",
"value": "26/11/18"
},
{
"name": "customerstatus",
"value": "active"
}
],
"firstname": "jane",
"lastname": "doe"
}
]
add a comment |
At little more complicated than needs to be, but you can try building your JSON array as you read in your values from the csv file, then output your result to a .json
file with json.dump
at the end:
from csv import reader
from json import dump
top_fields = ["firstname", "lastname", "email"]
extra_fields = ["customerid", "dateadded", "customerstatus"]
data =
with open("customers.csv") as csv_in:
csv_reader = reader(csv_in)
# Get headers
headers = list(map(str.strip, next(csv_reader)))
for row in csv_reader:
json_object = {}
# Build dictionary for each row
row_map = dict(zip(headers, map(str.strip, row)))
# Add in top fields first
for top in top_fields:
json_object[top] = row_map[top]
# Then add in extra fields
for extra in extra_fields:
json_object.setdefault("extraFields", ).append(
{"name": extra, "value": row_map[extra]}
)
data.append(json_object)
with open("customers.json", "w") as fp:
dump(data, fp, indent=4, sort_keys=True)
Which gives the following customers.json:
[
{
"email": "john.doe@do.com",
"extraFields": [
{
"name": "customerid",
"value": "124"
},
{
"name": "dateadded",
"value": "26/11/18"
},
{
"name": "customerstatus",
"value": "active"
}
],
"firstname": "john",
"lastname": "doe"
},
{
"email": "jane.doe@do.com",
"extraFields": [
{
"name": "customerid",
"value": "125"
},
{
"name": "dateadded",
"value": "26/11/18"
},
{
"name": "customerstatus",
"value": "active"
}
],
"firstname": "jane",
"lastname": "doe"
}
]
add a comment |
At little more complicated than needs to be, but you can try building your JSON array as you read in your values from the csv file, then output your result to a .json
file with json.dump
at the end:
from csv import reader
from json import dump
top_fields = ["firstname", "lastname", "email"]
extra_fields = ["customerid", "dateadded", "customerstatus"]
data =
with open("customers.csv") as csv_in:
csv_reader = reader(csv_in)
# Get headers
headers = list(map(str.strip, next(csv_reader)))
for row in csv_reader:
json_object = {}
# Build dictionary for each row
row_map = dict(zip(headers, map(str.strip, row)))
# Add in top fields first
for top in top_fields:
json_object[top] = row_map[top]
# Then add in extra fields
for extra in extra_fields:
json_object.setdefault("extraFields", ).append(
{"name": extra, "value": row_map[extra]}
)
data.append(json_object)
with open("customers.json", "w") as fp:
dump(data, fp, indent=4, sort_keys=True)
Which gives the following customers.json:
[
{
"email": "john.doe@do.com",
"extraFields": [
{
"name": "customerid",
"value": "124"
},
{
"name": "dateadded",
"value": "26/11/18"
},
{
"name": "customerstatus",
"value": "active"
}
],
"firstname": "john",
"lastname": "doe"
},
{
"email": "jane.doe@do.com",
"extraFields": [
{
"name": "customerid",
"value": "125"
},
{
"name": "dateadded",
"value": "26/11/18"
},
{
"name": "customerstatus",
"value": "active"
}
],
"firstname": "jane",
"lastname": "doe"
}
]
At little more complicated than needs to be, but you can try building your JSON array as you read in your values from the csv file, then output your result to a .json
file with json.dump
at the end:
from csv import reader
from json import dump
top_fields = ["firstname", "lastname", "email"]
extra_fields = ["customerid", "dateadded", "customerstatus"]
data =
with open("customers.csv") as csv_in:
csv_reader = reader(csv_in)
# Get headers
headers = list(map(str.strip, next(csv_reader)))
for row in csv_reader:
json_object = {}
# Build dictionary for each row
row_map = dict(zip(headers, map(str.strip, row)))
# Add in top fields first
for top in top_fields:
json_object[top] = row_map[top]
# Then add in extra fields
for extra in extra_fields:
json_object.setdefault("extraFields", ).append(
{"name": extra, "value": row_map[extra]}
)
data.append(json_object)
with open("customers.json", "w") as fp:
dump(data, fp, indent=4, sort_keys=True)
Which gives the following customers.json:
[
{
"email": "john.doe@do.com",
"extraFields": [
{
"name": "customerid",
"value": "124"
},
{
"name": "dateadded",
"value": "26/11/18"
},
{
"name": "customerstatus",
"value": "active"
}
],
"firstname": "john",
"lastname": "doe"
},
{
"email": "jane.doe@do.com",
"extraFields": [
{
"name": "customerid",
"value": "125"
},
{
"name": "dateadded",
"value": "26/11/18"
},
{
"name": "customerstatus",
"value": "active"
}
],
"firstname": "jane",
"lastname": "doe"
}
]
edited Nov 26 '18 at 3:42
answered Nov 26 '18 at 3:24
RoadRunnerRoadRunner
11.3k31341
11.3k31341
add a comment |
add a comment |
import csv
import sys
import json
#EDIT THIS LIST WITH YOUR REQUIRED JSON KEY NAMES
fieldnames=["firstname","secondname","age"]
def convert(filename):
csv_filename = filename[0]
print "Opening CSV file: ",csv_filename
f=open(csv_filename, 'r')
csv_reader = csv.DictReader(f,fieldnames)
json_filename = csv_filename.split(".")[0]+".json"
print "Saving JSON to file: ",json_filename
jsonf = open(json_filename,'w')
data = json.dumps([r for r in csv_reader])
jsonf.write(data)
f.close()
jsonf.close()
if __name__=="__main__":
convert(sys.argv[1:])
USAGE:
python csv2json.py myCSVfile.txt
where myCSVfile.txt it's your CSV file (name it as you prefer).
It will create a JSON array in a file named myCSVfile.json
That's all.
add a comment |
import csv
import sys
import json
#EDIT THIS LIST WITH YOUR REQUIRED JSON KEY NAMES
fieldnames=["firstname","secondname","age"]
def convert(filename):
csv_filename = filename[0]
print "Opening CSV file: ",csv_filename
f=open(csv_filename, 'r')
csv_reader = csv.DictReader(f,fieldnames)
json_filename = csv_filename.split(".")[0]+".json"
print "Saving JSON to file: ",json_filename
jsonf = open(json_filename,'w')
data = json.dumps([r for r in csv_reader])
jsonf.write(data)
f.close()
jsonf.close()
if __name__=="__main__":
convert(sys.argv[1:])
USAGE:
python csv2json.py myCSVfile.txt
where myCSVfile.txt it's your CSV file (name it as you prefer).
It will create a JSON array in a file named myCSVfile.json
That's all.
add a comment |
import csv
import sys
import json
#EDIT THIS LIST WITH YOUR REQUIRED JSON KEY NAMES
fieldnames=["firstname","secondname","age"]
def convert(filename):
csv_filename = filename[0]
print "Opening CSV file: ",csv_filename
f=open(csv_filename, 'r')
csv_reader = csv.DictReader(f,fieldnames)
json_filename = csv_filename.split(".")[0]+".json"
print "Saving JSON to file: ",json_filename
jsonf = open(json_filename,'w')
data = json.dumps([r for r in csv_reader])
jsonf.write(data)
f.close()
jsonf.close()
if __name__=="__main__":
convert(sys.argv[1:])
USAGE:
python csv2json.py myCSVfile.txt
where myCSVfile.txt it's your CSV file (name it as you prefer).
It will create a JSON array in a file named myCSVfile.json
That's all.
import csv
import sys
import json
#EDIT THIS LIST WITH YOUR REQUIRED JSON KEY NAMES
fieldnames=["firstname","secondname","age"]
def convert(filename):
csv_filename = filename[0]
print "Opening CSV file: ",csv_filename
f=open(csv_filename, 'r')
csv_reader = csv.DictReader(f,fieldnames)
json_filename = csv_filename.split(".")[0]+".json"
print "Saving JSON to file: ",json_filename
jsonf = open(json_filename,'w')
data = json.dumps([r for r in csv_reader])
jsonf.write(data)
f.close()
jsonf.close()
if __name__=="__main__":
convert(sys.argv[1:])
USAGE:
python csv2json.py myCSVfile.txt
where myCSVfile.txt it's your CSV file (name it as you prefer).
It will create a JSON array in a file named myCSVfile.json
That's all.
answered Nov 26 '18 at 4:34
SnivioSnivio
1066
1066
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474118%2fconvert-csv-to-json-nested-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you share your code so that we can update it from there
– Krishna
Nov 26 '18 at 2:55
Hi I have added my code in the question above. That might not help as I am converting csv to json straight away.
– jdag
Nov 26 '18 at 3:19
Possible duplicate of How to convert CSV to JSON?
– SuperShoot
Nov 26 '18 at 3:20