Pyspark: How to convert a spark dataframe to json and save it as json file?












0















I am trying to convert my pyspark sql dataframe to json and then save as a file.



df_final = df_final.union(join_df)


df_final contains the value as such:



enter image description here



I tried something like this. But it created a invalid json.



df_final.coalesce(1).write.format('json').save(data_output_file+"createjson.json", overwrite=True)

{"Variable":"Col1","Min":"20","Max":"30"}
{"Variable":"Col2","Min":"25,"Max":"40"}


My expected file should have data as below:



[
{"Variable":"Col1",
"Min":"20",
"Max":"30"},
{"Variable":"Col2",
"Min":"25,
"Max":"40"}]









share|improve this question























  • try df.toJSON()

    – Bala
    Nov 22 '18 at 10:09
















0















I am trying to convert my pyspark sql dataframe to json and then save as a file.



df_final = df_final.union(join_df)


df_final contains the value as such:



enter image description here



I tried something like this. But it created a invalid json.



df_final.coalesce(1).write.format('json').save(data_output_file+"createjson.json", overwrite=True)

{"Variable":"Col1","Min":"20","Max":"30"}
{"Variable":"Col2","Min":"25,"Max":"40"}


My expected file should have data as below:



[
{"Variable":"Col1",
"Min":"20",
"Max":"30"},
{"Variable":"Col2",
"Min":"25,
"Max":"40"}]









share|improve this question























  • try df.toJSON()

    – Bala
    Nov 22 '18 at 10:09














0












0








0








I am trying to convert my pyspark sql dataframe to json and then save as a file.



df_final = df_final.union(join_df)


df_final contains the value as such:



enter image description here



I tried something like this. But it created a invalid json.



df_final.coalesce(1).write.format('json').save(data_output_file+"createjson.json", overwrite=True)

{"Variable":"Col1","Min":"20","Max":"30"}
{"Variable":"Col2","Min":"25,"Max":"40"}


My expected file should have data as below:



[
{"Variable":"Col1",
"Min":"20",
"Max":"30"},
{"Variable":"Col2",
"Min":"25,
"Max":"40"}]









share|improve this question














I am trying to convert my pyspark sql dataframe to json and then save as a file.



df_final = df_final.union(join_df)


df_final contains the value as such:



enter image description here



I tried something like this. But it created a invalid json.



df_final.coalesce(1).write.format('json').save(data_output_file+"createjson.json", overwrite=True)

{"Variable":"Col1","Min":"20","Max":"30"}
{"Variable":"Col2","Min":"25,"Max":"40"}


My expected file should have data as below:



[
{"Variable":"Col1",
"Min":"20",
"Max":"30"},
{"Variable":"Col2",
"Min":"25,
"Max":"40"}]






python-3.x pyspark apache-spark-sql pyspark-sql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 8:10









Shankar PandaShankar Panda

1741114




1741114













  • try df.toJSON()

    – Bala
    Nov 22 '18 at 10:09



















  • try df.toJSON()

    – Bala
    Nov 22 '18 at 10:09

















try df.toJSON()

– Bala
Nov 22 '18 at 10:09





try df.toJSON()

– Bala
Nov 22 '18 at 10:09












3 Answers
3






active

oldest

votes


















2














A solution can be using collect and then using json.dump:



import json
collected_df = df_final.collect()
with open(data_output_file + 'createjson.json', 'w') as outfile:
json.dump(data, outfile)





share|improve this answer
























  • Actually this correct but it is not creating the file directly in hdfs. It creates on the container where the script runs

    – Shankar Panda
    Nov 22 '18 at 9:41





















1














If you want to use spark to process result as json files, I think that your output schema is right in hdfs.



And I assumed you encountered the issue that you can not smoothly read data from normal python script by using :



with open('data.json') as f:
data = json.load(f)


You should try to read data line by line:



data = 
with open("data.json",'r') as datafile:
for line in datafile:
data.append(json.loads(line))


and you can use pandas to create dataframe :



df = pd.DataFrame(data) 





share|improve this answer































    0














    For pyspark you can directly store your dataframe into json file, there is no need to convert the datafram into json.



    df_final.coalesce(1).write.format('json').save('/path/file_name.json')


    and still you want to convert your datafram into json then you can used
    df_final.toJSON().






    share|improve this answer
























    • Yeah, but it stores data line by line {"Variable":"Col1","Min":"20","Max":"30"} {"Variable":"Col2","Min":"25,"Max":"40"} instead it should be separated by , and enclosed with square braces

      – Shankar Panda
      Nov 23 '18 at 7:48











    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%2f53426420%2fpyspark-how-to-convert-a-spark-dataframe-to-json-and-save-it-as-json-file%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














    A solution can be using collect and then using json.dump:



    import json
    collected_df = df_final.collect()
    with open(data_output_file + 'createjson.json', 'w') as outfile:
    json.dump(data, outfile)





    share|improve this answer
























    • Actually this correct but it is not creating the file directly in hdfs. It creates on the container where the script runs

      – Shankar Panda
      Nov 22 '18 at 9:41


















    2














    A solution can be using collect and then using json.dump:



    import json
    collected_df = df_final.collect()
    with open(data_output_file + 'createjson.json', 'w') as outfile:
    json.dump(data, outfile)





    share|improve this answer
























    • Actually this correct but it is not creating the file directly in hdfs. It creates on the container where the script runs

      – Shankar Panda
      Nov 22 '18 at 9:41
















    2












    2








    2







    A solution can be using collect and then using json.dump:



    import json
    collected_df = df_final.collect()
    with open(data_output_file + 'createjson.json', 'w') as outfile:
    json.dump(data, outfile)





    share|improve this answer













    A solution can be using collect and then using json.dump:



    import json
    collected_df = df_final.collect()
    with open(data_output_file + 'createjson.json', 'w') as outfile:
    json.dump(data, outfile)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 '18 at 9:40









    OmGOmG

    7,97852743




    7,97852743













    • Actually this correct but it is not creating the file directly in hdfs. It creates on the container where the script runs

      – Shankar Panda
      Nov 22 '18 at 9:41





















    • Actually this correct but it is not creating the file directly in hdfs. It creates on the container where the script runs

      – Shankar Panda
      Nov 22 '18 at 9:41



















    Actually this correct but it is not creating the file directly in hdfs. It creates on the container where the script runs

    – Shankar Panda
    Nov 22 '18 at 9:41







    Actually this correct but it is not creating the file directly in hdfs. It creates on the container where the script runs

    – Shankar Panda
    Nov 22 '18 at 9:41















    1














    If you want to use spark to process result as json files, I think that your output schema is right in hdfs.



    And I assumed you encountered the issue that you can not smoothly read data from normal python script by using :



    with open('data.json') as f:
    data = json.load(f)


    You should try to read data line by line:



    data = 
    with open("data.json",'r') as datafile:
    for line in datafile:
    data.append(json.loads(line))


    and you can use pandas to create dataframe :



    df = pd.DataFrame(data) 





    share|improve this answer




























      1














      If you want to use spark to process result as json files, I think that your output schema is right in hdfs.



      And I assumed you encountered the issue that you can not smoothly read data from normal python script by using :



      with open('data.json') as f:
      data = json.load(f)


      You should try to read data line by line:



      data = 
      with open("data.json",'r') as datafile:
      for line in datafile:
      data.append(json.loads(line))


      and you can use pandas to create dataframe :



      df = pd.DataFrame(data) 





      share|improve this answer


























        1












        1








        1







        If you want to use spark to process result as json files, I think that your output schema is right in hdfs.



        And I assumed you encountered the issue that you can not smoothly read data from normal python script by using :



        with open('data.json') as f:
        data = json.load(f)


        You should try to read data line by line:



        data = 
        with open("data.json",'r') as datafile:
        for line in datafile:
        data.append(json.loads(line))


        and you can use pandas to create dataframe :



        df = pd.DataFrame(data) 





        share|improve this answer













        If you want to use spark to process result as json files, I think that your output schema is right in hdfs.



        And I assumed you encountered the issue that you can not smoothly read data from normal python script by using :



        with open('data.json') as f:
        data = json.load(f)


        You should try to read data line by line:



        data = 
        with open("data.json",'r') as datafile:
        for line in datafile:
        data.append(json.loads(line))


        and you can use pandas to create dataframe :



        df = pd.DataFrame(data) 






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 2:49









        chilunchilun

        948




        948























            0














            For pyspark you can directly store your dataframe into json file, there is no need to convert the datafram into json.



            df_final.coalesce(1).write.format('json').save('/path/file_name.json')


            and still you want to convert your datafram into json then you can used
            df_final.toJSON().






            share|improve this answer
























            • Yeah, but it stores data line by line {"Variable":"Col1","Min":"20","Max":"30"} {"Variable":"Col2","Min":"25,"Max":"40"} instead it should be separated by , and enclosed with square braces

              – Shankar Panda
              Nov 23 '18 at 7:48
















            0














            For pyspark you can directly store your dataframe into json file, there is no need to convert the datafram into json.



            df_final.coalesce(1).write.format('json').save('/path/file_name.json')


            and still you want to convert your datafram into json then you can used
            df_final.toJSON().






            share|improve this answer
























            • Yeah, but it stores data line by line {"Variable":"Col1","Min":"20","Max":"30"} {"Variable":"Col2","Min":"25,"Max":"40"} instead it should be separated by , and enclosed with square braces

              – Shankar Panda
              Nov 23 '18 at 7:48














            0












            0








            0







            For pyspark you can directly store your dataframe into json file, there is no need to convert the datafram into json.



            df_final.coalesce(1).write.format('json').save('/path/file_name.json')


            and still you want to convert your datafram into json then you can used
            df_final.toJSON().






            share|improve this answer













            For pyspark you can directly store your dataframe into json file, there is no need to convert the datafram into json.



            df_final.coalesce(1).write.format('json').save('/path/file_name.json')


            and still you want to convert your datafram into json then you can used
            df_final.toJSON().







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 7:40









            Sahil DesaiSahil Desai

            1,3893822




            1,3893822













            • Yeah, but it stores data line by line {"Variable":"Col1","Min":"20","Max":"30"} {"Variable":"Col2","Min":"25,"Max":"40"} instead it should be separated by , and enclosed with square braces

              – Shankar Panda
              Nov 23 '18 at 7:48



















            • Yeah, but it stores data line by line {"Variable":"Col1","Min":"20","Max":"30"} {"Variable":"Col2","Min":"25,"Max":"40"} instead it should be separated by , and enclosed with square braces

              – Shankar Panda
              Nov 23 '18 at 7:48

















            Yeah, but it stores data line by line {"Variable":"Col1","Min":"20","Max":"30"} {"Variable":"Col2","Min":"25,"Max":"40"} instead it should be separated by , and enclosed with square braces

            – Shankar Panda
            Nov 23 '18 at 7:48





            Yeah, but it stores data line by line {"Variable":"Col1","Min":"20","Max":"30"} {"Variable":"Col2","Min":"25,"Max":"40"} instead it should be separated by , and enclosed with square braces

            – Shankar Panda
            Nov 23 '18 at 7:48


















            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%2f53426420%2fpyspark-how-to-convert-a-spark-dataframe-to-json-and-save-it-as-json-file%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'