How to generate JSON from the csv file in Python?












3















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:



enter image description here



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)









share|improve this question




















  • 1





    What is res_df here?

    – Daniel Roseman
    Nov 16 '18 at 8:47
















3















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:



enter image description here



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)









share|improve this question




















  • 1





    What is res_df here?

    – Daniel Roseman
    Nov 16 '18 at 8:47














3












3








3








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:



enter image description here



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)









share|improve this question
















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:



enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 8:59







Shankar Panda

















asked Nov 16 '18 at 8:43









Shankar PandaShankar Panda

1691215




1691215








  • 1





    What is res_df here?

    – Daniel Roseman
    Nov 16 '18 at 8:47














  • 1





    What is res_df here?

    – 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












3 Answers
3






active

oldest

votes


















2














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}]





share|improve this answer
























  • Perfect. Simple and best

    – Shankar Panda
    Nov 16 '18 at 9:01



















0














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')





share|improve this answer
























  • 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



















0














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






share|improve this answer
























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    2














    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}]





    share|improve this answer
























    • Perfect. Simple and best

      – Shankar Panda
      Nov 16 '18 at 9:01
















    2














    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}]





    share|improve this answer
























    • Perfect. Simple and best

      – Shankar Panda
      Nov 16 '18 at 9:01














    2












    2








    2







    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}]





    share|improve this answer













    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}]






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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



















    • 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













    0














    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')





    share|improve this answer
























    • 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
















    0














    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')





    share|improve this answer
























    • 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














    0












    0








    0







    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')





    share|improve this answer













    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')






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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



















    • 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











    0














    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






    share|improve this answer




























      0














      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






      share|improve this answer


























        0












        0








        0







        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






        share|improve this answer













        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 9:04









        Harsha BiyaniHarsha Biyani

        2,72321834




        2,72321834






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly