How to convert CSV to JSON using Python?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I need to convert data.csv file to an "ExpectedJsonFile.json
" file using python script which is specified below. But I fail to achieve this. Python script "csvjs.py
" is specified as below.
import pandas as pd
from itertools import groupby
from collections import OrderedDict
import json
df = pd.read_csv('data8.csv', dtype=
"Source" : str,
"Template": str,
"ConfigurationSetName": str,
)
results =
for (Source, Template, ConfigurationSetName), bag in df.groupby (["Source", "Template", "ConfigurationSetName"]):
contents_df = bag.drop(["Source", "Template", "ConfigurationSetName"], axis=1)
Destinations = [OrderedDict(row) for i,row in contents_df.iterrows()]
results.append(OrderedDict([("Source", Source),
("Template", Template),
("ConfigurationSetName", ConfigurationSetName),
("Destinations", Destinations)]))
print json.dumps(results[0], indent=4)
with open('ExpectedJsonFile.json', 'w') as outfile:
outfile.write(json.dumps(results[0], indent=4))
data in data.csv
look like below.
Source,Template,ConfigurationSetName,ToAddresses,ReplacementTemplateData
demo@example.com,MyTemplate,noreply,customer1@gmail.com,customer1
demo@example.com,MyTemplate,noreply,customer2@gmail.com,customer2
Output Produces is like below when I run "python csvjs.py
"
"Source": "demo@example.com",
"Template": "MyTemplate",
"ConfigurationSetName": "noreply",
"Destinations": [
"ToAddresses": "customer1@gmail.com",
"ReplacementTemplateData": "customer"
,
"ToAddresses": "customer2@gmail.com",
"ReplacementTemplateData": "customer2"
]
But my expected output is as below
"Source":"demo@example.com",
"Template":"MyTemplate",
"ConfigurationSetName": "noreply",
"Destinations":[
"Destination":
"ToAddresses":[
"customer1@gmail.com"
]
,
"ReplacementTemplateData":" "name":"customer1" "
,
"Destination":
"ToAddresses":[
"customer2@gmail.com"
]
,
"ReplacementTemplateData":" "name":"customer2" "
,
"Destination":
"ToAddresses":[
"customer3@gmail.com"
]
,
"ReplacementTemplateData":""
],
"DefaultTemplateData":" "name":"friend" "
My template looks like below
"Template":
"TemplateName": "MyTemplate",
"SubjectPart": "Greetings, Name!",
"HtmlPart": "<h1>Hello Name,</h1><p>Your favorite animal is cat.</p>",
"TextPart": "Dear Name,rnYour favorite animal is cat."
python json pandas csv
add a comment |
I need to convert data.csv file to an "ExpectedJsonFile.json
" file using python script which is specified below. But I fail to achieve this. Python script "csvjs.py
" is specified as below.
import pandas as pd
from itertools import groupby
from collections import OrderedDict
import json
df = pd.read_csv('data8.csv', dtype=
"Source" : str,
"Template": str,
"ConfigurationSetName": str,
)
results =
for (Source, Template, ConfigurationSetName), bag in df.groupby (["Source", "Template", "ConfigurationSetName"]):
contents_df = bag.drop(["Source", "Template", "ConfigurationSetName"], axis=1)
Destinations = [OrderedDict(row) for i,row in contents_df.iterrows()]
results.append(OrderedDict([("Source", Source),
("Template", Template),
("ConfigurationSetName", ConfigurationSetName),
("Destinations", Destinations)]))
print json.dumps(results[0], indent=4)
with open('ExpectedJsonFile.json', 'w') as outfile:
outfile.write(json.dumps(results[0], indent=4))
data in data.csv
look like below.
Source,Template,ConfigurationSetName,ToAddresses,ReplacementTemplateData
demo@example.com,MyTemplate,noreply,customer1@gmail.com,customer1
demo@example.com,MyTemplate,noreply,customer2@gmail.com,customer2
Output Produces is like below when I run "python csvjs.py
"
"Source": "demo@example.com",
"Template": "MyTemplate",
"ConfigurationSetName": "noreply",
"Destinations": [
"ToAddresses": "customer1@gmail.com",
"ReplacementTemplateData": "customer"
,
"ToAddresses": "customer2@gmail.com",
"ReplacementTemplateData": "customer2"
]
But my expected output is as below
"Source":"demo@example.com",
"Template":"MyTemplate",
"ConfigurationSetName": "noreply",
"Destinations":[
"Destination":
"ToAddresses":[
"customer1@gmail.com"
]
,
"ReplacementTemplateData":" "name":"customer1" "
,
"Destination":
"ToAddresses":[
"customer2@gmail.com"
]
,
"ReplacementTemplateData":" "name":"customer2" "
,
"Destination":
"ToAddresses":[
"customer3@gmail.com"
]
,
"ReplacementTemplateData":""
],
"DefaultTemplateData":" "name":"friend" "
My template looks like below
"Template":
"TemplateName": "MyTemplate",
"SubjectPart": "Greetings, Name!",
"HtmlPart": "<h1>Hello Name,</h1><p>Your favorite animal is cat.</p>",
"TextPart": "Dear Name,rnYour favorite animal is cat."
python json pandas csv
Do you really need this"Destination":{
key? Seems just like noise within the outer array
– cricket_007
Nov 15 '18 at 7:29
Yes I need that
– Shyamkumar Chauhan
Nov 15 '18 at 11:46
Okay, well, I suggest rewriting theDestinations = [OrderedDict(row)
line such that you can actually create the intermediateDestination
object within the JSON and soToAddresses
can become a list rather than a string
– cricket_007
Nov 15 '18 at 15:20
add a comment |
I need to convert data.csv file to an "ExpectedJsonFile.json
" file using python script which is specified below. But I fail to achieve this. Python script "csvjs.py
" is specified as below.
import pandas as pd
from itertools import groupby
from collections import OrderedDict
import json
df = pd.read_csv('data8.csv', dtype=
"Source" : str,
"Template": str,
"ConfigurationSetName": str,
)
results =
for (Source, Template, ConfigurationSetName), bag in df.groupby (["Source", "Template", "ConfigurationSetName"]):
contents_df = bag.drop(["Source", "Template", "ConfigurationSetName"], axis=1)
Destinations = [OrderedDict(row) for i,row in contents_df.iterrows()]
results.append(OrderedDict([("Source", Source),
("Template", Template),
("ConfigurationSetName", ConfigurationSetName),
("Destinations", Destinations)]))
print json.dumps(results[0], indent=4)
with open('ExpectedJsonFile.json', 'w') as outfile:
outfile.write(json.dumps(results[0], indent=4))
data in data.csv
look like below.
Source,Template,ConfigurationSetName,ToAddresses,ReplacementTemplateData
demo@example.com,MyTemplate,noreply,customer1@gmail.com,customer1
demo@example.com,MyTemplate,noreply,customer2@gmail.com,customer2
Output Produces is like below when I run "python csvjs.py
"
"Source": "demo@example.com",
"Template": "MyTemplate",
"ConfigurationSetName": "noreply",
"Destinations": [
"ToAddresses": "customer1@gmail.com",
"ReplacementTemplateData": "customer"
,
"ToAddresses": "customer2@gmail.com",
"ReplacementTemplateData": "customer2"
]
But my expected output is as below
"Source":"demo@example.com",
"Template":"MyTemplate",
"ConfigurationSetName": "noreply",
"Destinations":[
"Destination":
"ToAddresses":[
"customer1@gmail.com"
]
,
"ReplacementTemplateData":" "name":"customer1" "
,
"Destination":
"ToAddresses":[
"customer2@gmail.com"
]
,
"ReplacementTemplateData":" "name":"customer2" "
,
"Destination":
"ToAddresses":[
"customer3@gmail.com"
]
,
"ReplacementTemplateData":""
],
"DefaultTemplateData":" "name":"friend" "
My template looks like below
"Template":
"TemplateName": "MyTemplate",
"SubjectPart": "Greetings, Name!",
"HtmlPart": "<h1>Hello Name,</h1><p>Your favorite animal is cat.</p>",
"TextPart": "Dear Name,rnYour favorite animal is cat."
python json pandas csv
I need to convert data.csv file to an "ExpectedJsonFile.json
" file using python script which is specified below. But I fail to achieve this. Python script "csvjs.py
" is specified as below.
import pandas as pd
from itertools import groupby
from collections import OrderedDict
import json
df = pd.read_csv('data8.csv', dtype=
"Source" : str,
"Template": str,
"ConfigurationSetName": str,
)
results =
for (Source, Template, ConfigurationSetName), bag in df.groupby (["Source", "Template", "ConfigurationSetName"]):
contents_df = bag.drop(["Source", "Template", "ConfigurationSetName"], axis=1)
Destinations = [OrderedDict(row) for i,row in contents_df.iterrows()]
results.append(OrderedDict([("Source", Source),
("Template", Template),
("ConfigurationSetName", ConfigurationSetName),
("Destinations", Destinations)]))
print json.dumps(results[0], indent=4)
with open('ExpectedJsonFile.json', 'w') as outfile:
outfile.write(json.dumps(results[0], indent=4))
data in data.csv
look like below.
Source,Template,ConfigurationSetName,ToAddresses,ReplacementTemplateData
demo@example.com,MyTemplate,noreply,customer1@gmail.com,customer1
demo@example.com,MyTemplate,noreply,customer2@gmail.com,customer2
Output Produces is like below when I run "python csvjs.py
"
"Source": "demo@example.com",
"Template": "MyTemplate",
"ConfigurationSetName": "noreply",
"Destinations": [
"ToAddresses": "customer1@gmail.com",
"ReplacementTemplateData": "customer"
,
"ToAddresses": "customer2@gmail.com",
"ReplacementTemplateData": "customer2"
]
But my expected output is as below
"Source":"demo@example.com",
"Template":"MyTemplate",
"ConfigurationSetName": "noreply",
"Destinations":[
"Destination":
"ToAddresses":[
"customer1@gmail.com"
]
,
"ReplacementTemplateData":" "name":"customer1" "
,
"Destination":
"ToAddresses":[
"customer2@gmail.com"
]
,
"ReplacementTemplateData":" "name":"customer2" "
,
"Destination":
"ToAddresses":[
"customer3@gmail.com"
]
,
"ReplacementTemplateData":""
],
"DefaultTemplateData":" "name":"friend" "
My template looks like below
"Template":
"TemplateName": "MyTemplate",
"SubjectPart": "Greetings, Name!",
"HtmlPart": "<h1>Hello Name,</h1><p>Your favorite animal is cat.</p>",
"TextPart": "Dear Name,rnYour favorite animal is cat."
python json pandas csv
python json pandas csv
edited Nov 15 '18 at 22:23
halfer
14.7k759116
14.7k759116
asked Nov 15 '18 at 6:58
Shyamkumar ChauhanShyamkumar Chauhan
61
61
Do you really need this"Destination":{
key? Seems just like noise within the outer array
– cricket_007
Nov 15 '18 at 7:29
Yes I need that
– Shyamkumar Chauhan
Nov 15 '18 at 11:46
Okay, well, I suggest rewriting theDestinations = [OrderedDict(row)
line such that you can actually create the intermediateDestination
object within the JSON and soToAddresses
can become a list rather than a string
– cricket_007
Nov 15 '18 at 15:20
add a comment |
Do you really need this"Destination":{
key? Seems just like noise within the outer array
– cricket_007
Nov 15 '18 at 7:29
Yes I need that
– Shyamkumar Chauhan
Nov 15 '18 at 11:46
Okay, well, I suggest rewriting theDestinations = [OrderedDict(row)
line such that you can actually create the intermediateDestination
object within the JSON and soToAddresses
can become a list rather than a string
– cricket_007
Nov 15 '18 at 15:20
Do you really need this
"Destination":{
key? Seems just like noise within the outer array– cricket_007
Nov 15 '18 at 7:29
Do you really need this
"Destination":{
key? Seems just like noise within the outer array– cricket_007
Nov 15 '18 at 7:29
Yes I need that
– Shyamkumar Chauhan
Nov 15 '18 at 11:46
Yes I need that
– Shyamkumar Chauhan
Nov 15 '18 at 11:46
Okay, well, I suggest rewriting the
Destinations = [OrderedDict(row)
line such that you can actually create the intermediate Destination
object within the JSON and so ToAddresses
can become a list rather than a string– cricket_007
Nov 15 '18 at 15:20
Okay, well, I suggest rewriting the
Destinations = [OrderedDict(row)
line such that you can actually create the intermediate Destination
object within the JSON and so ToAddresses
can become a list rather than a string– cricket_007
Nov 15 '18 at 15:20
add a comment |
1 Answer
1
active
oldest
votes
I partially succeed by changing this line code
contents_df = bag.drop(["Source", "Template", "ConfigurationSetName", "ToAddresses", "ReplacementTemplateData"], axis=1)
and the output produced now looks likes this .
"Source":"demo@example.com",
"Template":"MyTemplate",
"ConfigurationSetName": "noreply",
"Destinations":[
"Destination":
"ToAddresses":"customer1@gmail.com"
,
"Destination":
"ToAddresses":"customer2@gmail.com"
]
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%2f53313990%2fhow-to-convert-csv-to-json-using-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I partially succeed by changing this line code
contents_df = bag.drop(["Source", "Template", "ConfigurationSetName", "ToAddresses", "ReplacementTemplateData"], axis=1)
and the output produced now looks likes this .
"Source":"demo@example.com",
"Template":"MyTemplate",
"ConfigurationSetName": "noreply",
"Destinations":[
"Destination":
"ToAddresses":"customer1@gmail.com"
,
"Destination":
"ToAddresses":"customer2@gmail.com"
]
add a comment |
I partially succeed by changing this line code
contents_df = bag.drop(["Source", "Template", "ConfigurationSetName", "ToAddresses", "ReplacementTemplateData"], axis=1)
and the output produced now looks likes this .
"Source":"demo@example.com",
"Template":"MyTemplate",
"ConfigurationSetName": "noreply",
"Destinations":[
"Destination":
"ToAddresses":"customer1@gmail.com"
,
"Destination":
"ToAddresses":"customer2@gmail.com"
]
add a comment |
I partially succeed by changing this line code
contents_df = bag.drop(["Source", "Template", "ConfigurationSetName", "ToAddresses", "ReplacementTemplateData"], axis=1)
and the output produced now looks likes this .
"Source":"demo@example.com",
"Template":"MyTemplate",
"ConfigurationSetName": "noreply",
"Destinations":[
"Destination":
"ToAddresses":"customer1@gmail.com"
,
"Destination":
"ToAddresses":"customer2@gmail.com"
]
I partially succeed by changing this line code
contents_df = bag.drop(["Source", "Template", "ConfigurationSetName", "ToAddresses", "ReplacementTemplateData"], axis=1)
and the output produced now looks likes this .
"Source":"demo@example.com",
"Template":"MyTemplate",
"ConfigurationSetName": "noreply",
"Destinations":[
"Destination":
"ToAddresses":"customer1@gmail.com"
,
"Destination":
"ToAddresses":"customer2@gmail.com"
]
answered Nov 21 '18 at 9:54
Shyamkumar ChauhanShyamkumar Chauhan
61
61
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%2f53313990%2fhow-to-convert-csv-to-json-using-python%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
Do you really need this
"Destination":{
key? Seems just like noise within the outer array– cricket_007
Nov 15 '18 at 7:29
Yes I need that
– Shyamkumar Chauhan
Nov 15 '18 at 11:46
Okay, well, I suggest rewriting the
Destinations = [OrderedDict(row)
line such that you can actually create the intermediateDestination
object within the JSON and soToAddresses
can become a list rather than a string– cricket_007
Nov 15 '18 at 15:20