How to generate JSON from the csv file in Python?
I am trying to construct a Json structure from the csv file. This below code gives me the error stating :- AttributeError: 'tuple' object has no attribute 'to_json' . I am new to python world and would like to ask for your help on this.
CSV Data Look like this:

I want output to be like below
[
{"Variable": "Latitude",
"Min": "78",
"Q1": "89"} ,
{"Variable": "Longitude",
"Min": "78",
"Q1": "89"},
{"Variable": "Zip",
"Min": "78",
"Q1": "89"}
]
import pandas
res_data = pd.read_csv("C\Documents\abc.csv", 'r')
abc=res_data.to_json(orient='records')
print(abc)
python json python-3.x python-2.7 pandas
add a comment |
I am trying to construct a Json structure from the csv file. This below code gives me the error stating :- AttributeError: 'tuple' object has no attribute 'to_json' . I am new to python world and would like to ask for your help on this.
CSV Data Look like this:

I want output to be like below
[
{"Variable": "Latitude",
"Min": "78",
"Q1": "89"} ,
{"Variable": "Longitude",
"Min": "78",
"Q1": "89"},
{"Variable": "Zip",
"Min": "78",
"Q1": "89"}
]
import pandas
res_data = pd.read_csv("C\Documents\abc.csv", 'r')
abc=res_data.to_json(orient='records')
print(abc)
python json python-3.x python-2.7 pandas
1
What isres_dfhere?
– Daniel Roseman
Nov 16 '18 at 8:47
add a comment |
I am trying to construct a Json structure from the csv file. This below code gives me the error stating :- AttributeError: 'tuple' object has no attribute 'to_json' . I am new to python world and would like to ask for your help on this.
CSV Data Look like this:

I want output to be like below
[
{"Variable": "Latitude",
"Min": "78",
"Q1": "89"} ,
{"Variable": "Longitude",
"Min": "78",
"Q1": "89"},
{"Variable": "Zip",
"Min": "78",
"Q1": "89"}
]
import pandas
res_data = pd.read_csv("C\Documents\abc.csv", 'r')
abc=res_data.to_json(orient='records')
print(abc)
python json python-3.x python-2.7 pandas
I am trying to construct a Json structure from the csv file. This below code gives me the error stating :- AttributeError: 'tuple' object has no attribute 'to_json' . I am new to python world and would like to ask for your help on this.
CSV Data Look like this:

I want output to be like below
[
{"Variable": "Latitude",
"Min": "78",
"Q1": "89"} ,
{"Variable": "Longitude",
"Min": "78",
"Q1": "89"},
{"Variable": "Zip",
"Min": "78",
"Q1": "89"}
]
import pandas
res_data = pd.read_csv("C\Documents\abc.csv", 'r')
abc=res_data.to_json(orient='records')
print(abc)
python json python-3.x python-2.7 pandas
python json python-3.x python-2.7 pandas
edited Nov 16 '18 at 8:59
Shankar Panda
asked Nov 16 '18 at 8:43
Shankar PandaShankar Panda
1691215
1691215
1
What isres_dfhere?
– Daniel Roseman
Nov 16 '18 at 8:47
add a comment |
1
What isres_dfhere?
– Daniel Roseman
Nov 16 '18 at 8:47
1
1
What is
res_df here?– Daniel Roseman
Nov 16 '18 at 8:47
What is
res_df here?– Daniel Roseman
Nov 16 '18 at 8:47
add a comment |
3 Answers
3
active
oldest
votes
import json
import pandas as pd
df = pd.read_csv("path_of_csv")
js = df.to_json(orient="records")
json.loads(js)
Output:
[{'variable': 'Latitude', 'min': 26.84505, 'Q1': 31.19725},
{'variable': 'Longtitude', 'min': -122.315, 'Q1': -116.558},
{'variable': 'Zip', 'min': 20910.0, 'Q1': 32788.5}]
Perfect. Simple and best
– Shankar Panda
Nov 16 '18 at 9:01
add a comment |
Something like
import csv
import json
csvfile = open('file.csv', 'r')
jsonfile = open('file.json', 'w')
fieldnames = ("variable", "min", "Q1")
reader = csv.DictReader( csvfile, fieldnames)
for row in reader:
json.dump(row, jsonfile)
jsonfile.write('n')
It works but not generating the proper json structure. There should be comma separated for each row and the whole json should be enclosed in
– Shankar Panda
Nov 16 '18 at 9:02
add a comment |
You can try simply using csv module.
import csv
import json
output_dict =
with open('abc.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
output_dict.append(row)
print json.dumps(output_dict)
output_dict will contain list of dict of include all rows. json.dumps will convert python dict to json.
and output will be:
[{'variable': 'Latitude', 'min': 26.84505, 'Q1': 31.19725},
{'variable': 'Longtitude', 'min': -122.315, 'Q1': -116.558},
{'variable': 'Zip', 'min': 20910.0, 'Q1': 32788.5}]
More details abou csv.DictReader : enter link description here
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%2f53334238%2fhow-to-generate-json-from-the-csv-file-in-python%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
import json
import pandas as pd
df = pd.read_csv("path_of_csv")
js = df.to_json(orient="records")
json.loads(js)
Output:
[{'variable': 'Latitude', 'min': 26.84505, 'Q1': 31.19725},
{'variable': 'Longtitude', 'min': -122.315, 'Q1': -116.558},
{'variable': 'Zip', 'min': 20910.0, 'Q1': 32788.5}]
Perfect. Simple and best
– Shankar Panda
Nov 16 '18 at 9:01
add a comment |
import json
import pandas as pd
df = pd.read_csv("path_of_csv")
js = df.to_json(orient="records")
json.loads(js)
Output:
[{'variable': 'Latitude', 'min': 26.84505, 'Q1': 31.19725},
{'variable': 'Longtitude', 'min': -122.315, 'Q1': -116.558},
{'variable': 'Zip', 'min': 20910.0, 'Q1': 32788.5}]
Perfect. Simple and best
– Shankar Panda
Nov 16 '18 at 9:01
add a comment |
import json
import pandas as pd
df = pd.read_csv("path_of_csv")
js = df.to_json(orient="records")
json.loads(js)
Output:
[{'variable': 'Latitude', 'min': 26.84505, 'Q1': 31.19725},
{'variable': 'Longtitude', 'min': -122.315, 'Q1': -116.558},
{'variable': 'Zip', 'min': 20910.0, 'Q1': 32788.5}]
import json
import pandas as pd
df = pd.read_csv("path_of_csv")
js = df.to_json(orient="records")
json.loads(js)
Output:
[{'variable': 'Latitude', 'min': 26.84505, 'Q1': 31.19725},
{'variable': 'Longtitude', 'min': -122.315, 'Q1': -116.558},
{'variable': 'Zip', 'min': 20910.0, 'Q1': 32788.5}]
answered Nov 16 '18 at 8:56
Srce CdeSrce Cde
1,184612
1,184612
Perfect. Simple and best
– Shankar Panda
Nov 16 '18 at 9:01
add a comment |
Perfect. Simple and best
– Shankar Panda
Nov 16 '18 at 9:01
Perfect. Simple and best
– Shankar Panda
Nov 16 '18 at 9:01
Perfect. Simple and best
– Shankar Panda
Nov 16 '18 at 9:01
add a comment |
Something like
import csv
import json
csvfile = open('file.csv', 'r')
jsonfile = open('file.json', 'w')
fieldnames = ("variable", "min", "Q1")
reader = csv.DictReader( csvfile, fieldnames)
for row in reader:
json.dump(row, jsonfile)
jsonfile.write('n')
It works but not generating the proper json structure. There should be comma separated for each row and the whole json should be enclosed in
– Shankar Panda
Nov 16 '18 at 9:02
add a comment |
Something like
import csv
import json
csvfile = open('file.csv', 'r')
jsonfile = open('file.json', 'w')
fieldnames = ("variable", "min", "Q1")
reader = csv.DictReader( csvfile, fieldnames)
for row in reader:
json.dump(row, jsonfile)
jsonfile.write('n')
It works but not generating the proper json structure. There should be comma separated for each row and the whole json should be enclosed in
– Shankar Panda
Nov 16 '18 at 9:02
add a comment |
Something like
import csv
import json
csvfile = open('file.csv', 'r')
jsonfile = open('file.json', 'w')
fieldnames = ("variable", "min", "Q1")
reader = csv.DictReader( csvfile, fieldnames)
for row in reader:
json.dump(row, jsonfile)
jsonfile.write('n')
Something like
import csv
import json
csvfile = open('file.csv', 'r')
jsonfile = open('file.json', 'w')
fieldnames = ("variable", "min", "Q1")
reader = csv.DictReader( csvfile, fieldnames)
for row in reader:
json.dump(row, jsonfile)
jsonfile.write('n')
answered Nov 16 '18 at 8:46
elkenelken
138110
138110
It works but not generating the proper json structure. There should be comma separated for each row and the whole json should be enclosed in
– Shankar Panda
Nov 16 '18 at 9:02
add a comment |
It works but not generating the proper json structure. There should be comma separated for each row and the whole json should be enclosed in
– Shankar Panda
Nov 16 '18 at 9:02
It works but not generating the proper json structure. There should be comma separated for each row and the whole json should be enclosed in
– Shankar Panda
Nov 16 '18 at 9:02
It works but not generating the proper json structure. There should be comma separated for each row and the whole json should be enclosed in
– Shankar Panda
Nov 16 '18 at 9:02
add a comment |
You can try simply using csv module.
import csv
import json
output_dict =
with open('abc.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
output_dict.append(row)
print json.dumps(output_dict)
output_dict will contain list of dict of include all rows. json.dumps will convert python dict to json.
and output will be:
[{'variable': 'Latitude', 'min': 26.84505, 'Q1': 31.19725},
{'variable': 'Longtitude', 'min': -122.315, 'Q1': -116.558},
{'variable': 'Zip', 'min': 20910.0, 'Q1': 32788.5}]
More details abou csv.DictReader : enter link description here
add a comment |
You can try simply using csv module.
import csv
import json
output_dict =
with open('abc.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
output_dict.append(row)
print json.dumps(output_dict)
output_dict will contain list of dict of include all rows. json.dumps will convert python dict to json.
and output will be:
[{'variable': 'Latitude', 'min': 26.84505, 'Q1': 31.19725},
{'variable': 'Longtitude', 'min': -122.315, 'Q1': -116.558},
{'variable': 'Zip', 'min': 20910.0, 'Q1': 32788.5}]
More details abou csv.DictReader : enter link description here
add a comment |
You can try simply using csv module.
import csv
import json
output_dict =
with open('abc.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
output_dict.append(row)
print json.dumps(output_dict)
output_dict will contain list of dict of include all rows. json.dumps will convert python dict to json.
and output will be:
[{'variable': 'Latitude', 'min': 26.84505, 'Q1': 31.19725},
{'variable': 'Longtitude', 'min': -122.315, 'Q1': -116.558},
{'variable': 'Zip', 'min': 20910.0, 'Q1': 32788.5}]
More details abou csv.DictReader : enter link description here
You can try simply using csv module.
import csv
import json
output_dict =
with open('abc.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
output_dict.append(row)
print json.dumps(output_dict)
output_dict will contain list of dict of include all rows. json.dumps will convert python dict to json.
and output will be:
[{'variable': 'Latitude', 'min': 26.84505, 'Q1': 31.19725},
{'variable': 'Longtitude', 'min': -122.315, 'Q1': -116.558},
{'variable': 'Zip', 'min': 20910.0, 'Q1': 32788.5}]
More details abou csv.DictReader : enter link description here
answered Nov 16 '18 at 9:04
Harsha BiyaniHarsha Biyani
2,72321834
2,72321834
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%2f53334238%2fhow-to-generate-json-from-the-csv-file-in-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
1
What is
res_dfhere?– Daniel Roseman
Nov 16 '18 at 8:47