How to save a json from dataframe with specific format?How do I format a Microsoft JSON date?How can I pretty-print JSON in a shell script?What is JSON and why would I use it?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?How do I return clean JSON from a WCF Service?How can I parse a JSON file with PHP?How can I pretty-print JSON using JavaScript?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?Post JSON using Python RequestsThe “right” JSON date format

Why would a propellor have blades of different lengths?

Misaligned Columns

I had an c.p.a file late returns, stating i would get money. but i.r.s. says they were filed too late

Can I deep fry food in butter instead of vegetable oil?

How did שְׁלֹמֹה (shlomo) become Solomon?

Bypass with wrong cvv of debit card and getting OTP

Olive oil in Japanese cooking

Phrase origin: "You ain't got to go home but you got to get out of here."

Do the 26 richest billionaires own as much wealth as the poorest 3.8 billion people?

Can I have a forest in the rain shadow of a mountain range?

Who pays for increased security measures on flights to the US?

Performance of loop vs expansion

Did Stalin kill all Soviet officers involved in the Winter War?

Find the closest HTML colour name

Is it possible that Curiosity measured its own methane or failed doing the spectrometry?

What are the differences of checking a self-signed certificate vs ignore it?

Does this circuit have marginal voltage level problem?

Which high-degree derivatives play an essential role?

Show that there are infinitely more problems than we will ever be able to compute

Where is read command?

Are the plates of a battery really charged?

What do you call the angle of the direction of an airplane?

What is meaning of 4 letter acronyms in Roman names like Titus Flavius T. f. T. n. Sabinus?

Phrasing "it says" or "it reads"



How to save a json from dataframe with specific format?


How do I format a Microsoft JSON date?How can I pretty-print JSON in a shell script?What is JSON and why would I use it?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?How do I return clean JSON from a WCF Service?How can I parse a JSON file with PHP?How can I pretty-print JSON using JavaScript?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?Post JSON using Python RequestsThe “right” JSON date format






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















i'm trying to save the following pyspark dataframe to json:



ID VARIABLE_1
1 1
2 0
3 0
4 1
5 0


but i need the json to be like:



 "1": "VARIABLE_1": 1 
"2": "VARIABLE_1": 0
"3": "VARIABLE_1": 0
"4": "VARIABLE_1": 1
"5": "VARIABLE_1": 0


When saving to json i get the following:



 "ID":"1", "VARIABLE_1": 1 
"ID":"2", "VARIABLE_1": 0
"ID":"3", "VARIABLE_1": 0
"ID":"4", "VARIABLE_1": 1
"ID":"5", "VARIABLE_1": 0


i'm trying to save with



df.write.mode("overwrite").format("json").save(json_path)


i tried using create_map and got a dataframe like this:



NEW_COLUMN_NAME
"1": "VARIABLE_1": 1
"2": "VARIABLE_1": 0
"3": "VARIABLE_1": 0
"4": "VARIABLE_1": 1
"5": "VARIABLE_1": 0



But when i try to save this new dataframe to json i get:



"NEW_COLUMN_NAME":"1":"VARIABLE_1":1
"NEW_COLUMN_NAME":"2":"VARIABLE_1":0
"NEW_COLUMN_NAME":"3":"VARIABLE_1":0
"NEW_COLUMN_NAME":"4":"VARIABLE_1":1
"NEW_COLUMN_NAME":"5":"VARIABLE_1":0


I don't really know what to do, someone have any idea?



Thanks!










share|improve this question






















  • json format will contain the name and values, its not possible to drop one name (ID) and retain other name (VARIABLE_1) while saving as json format

    – Ranga Vure
    Mar 25 at 18:55











  • Once you get your data in the format you wanted (using create_map) try using .write.text(json_path) to write the string to a file. Something like this: df.select("NEW_COLUMN_NAME").write.text(json_path)

    – pault
    Mar 25 at 19:12












  • when i try to write.text it says "Text data source does not support map" and if i try to Cast('string') it changes from "1": "VARIABLE_1": 1 to [1-> [VARIABLE_1-> 0]]

    – TNS
    Mar 25 at 19:59












  • My spark is version 2.4

    – TNS
    Mar 25 at 20:24











  • I'm not able to test on 2.4 but does to_json work on the MapType() column? df.select(to_json("NEW_COLUMN_NAME")).write.text(json_path). If not, it's a little hacky but you can build the string manually: df.select(concat(lit('"'), "ID", lit('":"VARIABLE_1":"'), "VARIABLE_1", lit('"')).write.text(json_path)

    – pault
    Mar 25 at 20:56

















2















i'm trying to save the following pyspark dataframe to json:



ID VARIABLE_1
1 1
2 0
3 0
4 1
5 0


but i need the json to be like:



 "1": "VARIABLE_1": 1 
"2": "VARIABLE_1": 0
"3": "VARIABLE_1": 0
"4": "VARIABLE_1": 1
"5": "VARIABLE_1": 0


When saving to json i get the following:



 "ID":"1", "VARIABLE_1": 1 
"ID":"2", "VARIABLE_1": 0
"ID":"3", "VARIABLE_1": 0
"ID":"4", "VARIABLE_1": 1
"ID":"5", "VARIABLE_1": 0


i'm trying to save with



df.write.mode("overwrite").format("json").save(json_path)


i tried using create_map and got a dataframe like this:



NEW_COLUMN_NAME
"1": "VARIABLE_1": 1
"2": "VARIABLE_1": 0
"3": "VARIABLE_1": 0
"4": "VARIABLE_1": 1
"5": "VARIABLE_1": 0



But when i try to save this new dataframe to json i get:



"NEW_COLUMN_NAME":"1":"VARIABLE_1":1
"NEW_COLUMN_NAME":"2":"VARIABLE_1":0
"NEW_COLUMN_NAME":"3":"VARIABLE_1":0
"NEW_COLUMN_NAME":"4":"VARIABLE_1":1
"NEW_COLUMN_NAME":"5":"VARIABLE_1":0


I don't really know what to do, someone have any idea?



Thanks!










share|improve this question






















  • json format will contain the name and values, its not possible to drop one name (ID) and retain other name (VARIABLE_1) while saving as json format

    – Ranga Vure
    Mar 25 at 18:55











  • Once you get your data in the format you wanted (using create_map) try using .write.text(json_path) to write the string to a file. Something like this: df.select("NEW_COLUMN_NAME").write.text(json_path)

    – pault
    Mar 25 at 19:12












  • when i try to write.text it says "Text data source does not support map" and if i try to Cast('string') it changes from "1": "VARIABLE_1": 1 to [1-> [VARIABLE_1-> 0]]

    – TNS
    Mar 25 at 19:59












  • My spark is version 2.4

    – TNS
    Mar 25 at 20:24











  • I'm not able to test on 2.4 but does to_json work on the MapType() column? df.select(to_json("NEW_COLUMN_NAME")).write.text(json_path). If not, it's a little hacky but you can build the string manually: df.select(concat(lit('"'), "ID", lit('":"VARIABLE_1":"'), "VARIABLE_1", lit('"')).write.text(json_path)

    – pault
    Mar 25 at 20:56













2












2








2


1






i'm trying to save the following pyspark dataframe to json:



ID VARIABLE_1
1 1
2 0
3 0
4 1
5 0


but i need the json to be like:



 "1": "VARIABLE_1": 1 
"2": "VARIABLE_1": 0
"3": "VARIABLE_1": 0
"4": "VARIABLE_1": 1
"5": "VARIABLE_1": 0


When saving to json i get the following:



 "ID":"1", "VARIABLE_1": 1 
"ID":"2", "VARIABLE_1": 0
"ID":"3", "VARIABLE_1": 0
"ID":"4", "VARIABLE_1": 1
"ID":"5", "VARIABLE_1": 0


i'm trying to save with



df.write.mode("overwrite").format("json").save(json_path)


i tried using create_map and got a dataframe like this:



NEW_COLUMN_NAME
"1": "VARIABLE_1": 1
"2": "VARIABLE_1": 0
"3": "VARIABLE_1": 0
"4": "VARIABLE_1": 1
"5": "VARIABLE_1": 0



But when i try to save this new dataframe to json i get:



"NEW_COLUMN_NAME":"1":"VARIABLE_1":1
"NEW_COLUMN_NAME":"2":"VARIABLE_1":0
"NEW_COLUMN_NAME":"3":"VARIABLE_1":0
"NEW_COLUMN_NAME":"4":"VARIABLE_1":1
"NEW_COLUMN_NAME":"5":"VARIABLE_1":0


I don't really know what to do, someone have any idea?



Thanks!










share|improve this question














i'm trying to save the following pyspark dataframe to json:



ID VARIABLE_1
1 1
2 0
3 0
4 1
5 0


but i need the json to be like:



 "1": "VARIABLE_1": 1 
"2": "VARIABLE_1": 0
"3": "VARIABLE_1": 0
"4": "VARIABLE_1": 1
"5": "VARIABLE_1": 0


When saving to json i get the following:



 "ID":"1", "VARIABLE_1": 1 
"ID":"2", "VARIABLE_1": 0
"ID":"3", "VARIABLE_1": 0
"ID":"4", "VARIABLE_1": 1
"ID":"5", "VARIABLE_1": 0


i'm trying to save with



df.write.mode("overwrite").format("json").save(json_path)


i tried using create_map and got a dataframe like this:



NEW_COLUMN_NAME
"1": "VARIABLE_1": 1
"2": "VARIABLE_1": 0
"3": "VARIABLE_1": 0
"4": "VARIABLE_1": 1
"5": "VARIABLE_1": 0



But when i try to save this new dataframe to json i get:



"NEW_COLUMN_NAME":"1":"VARIABLE_1":1
"NEW_COLUMN_NAME":"2":"VARIABLE_1":0
"NEW_COLUMN_NAME":"3":"VARIABLE_1":0
"NEW_COLUMN_NAME":"4":"VARIABLE_1":1
"NEW_COLUMN_NAME":"5":"VARIABLE_1":0


I don't really know what to do, someone have any idea?



Thanks!







json pyspark






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 25 at 18:21









TNSTNS

112 bronze badges




112 bronze badges












  • json format will contain the name and values, its not possible to drop one name (ID) and retain other name (VARIABLE_1) while saving as json format

    – Ranga Vure
    Mar 25 at 18:55











  • Once you get your data in the format you wanted (using create_map) try using .write.text(json_path) to write the string to a file. Something like this: df.select("NEW_COLUMN_NAME").write.text(json_path)

    – pault
    Mar 25 at 19:12












  • when i try to write.text it says "Text data source does not support map" and if i try to Cast('string') it changes from "1": "VARIABLE_1": 1 to [1-> [VARIABLE_1-> 0]]

    – TNS
    Mar 25 at 19:59












  • My spark is version 2.4

    – TNS
    Mar 25 at 20:24











  • I'm not able to test on 2.4 but does to_json work on the MapType() column? df.select(to_json("NEW_COLUMN_NAME")).write.text(json_path). If not, it's a little hacky but you can build the string manually: df.select(concat(lit('"'), "ID", lit('":"VARIABLE_1":"'), "VARIABLE_1", lit('"')).write.text(json_path)

    – pault
    Mar 25 at 20:56

















  • json format will contain the name and values, its not possible to drop one name (ID) and retain other name (VARIABLE_1) while saving as json format

    – Ranga Vure
    Mar 25 at 18:55











  • Once you get your data in the format you wanted (using create_map) try using .write.text(json_path) to write the string to a file. Something like this: df.select("NEW_COLUMN_NAME").write.text(json_path)

    – pault
    Mar 25 at 19:12












  • when i try to write.text it says "Text data source does not support map" and if i try to Cast('string') it changes from "1": "VARIABLE_1": 1 to [1-> [VARIABLE_1-> 0]]

    – TNS
    Mar 25 at 19:59












  • My spark is version 2.4

    – TNS
    Mar 25 at 20:24











  • I'm not able to test on 2.4 but does to_json work on the MapType() column? df.select(to_json("NEW_COLUMN_NAME")).write.text(json_path). If not, it's a little hacky but you can build the string manually: df.select(concat(lit('"'), "ID", lit('":"VARIABLE_1":"'), "VARIABLE_1", lit('"')).write.text(json_path)

    – pault
    Mar 25 at 20:56
















json format will contain the name and values, its not possible to drop one name (ID) and retain other name (VARIABLE_1) while saving as json format

– Ranga Vure
Mar 25 at 18:55





json format will contain the name and values, its not possible to drop one name (ID) and retain other name (VARIABLE_1) while saving as json format

– Ranga Vure
Mar 25 at 18:55













Once you get your data in the format you wanted (using create_map) try using .write.text(json_path) to write the string to a file. Something like this: df.select("NEW_COLUMN_NAME").write.text(json_path)

– pault
Mar 25 at 19:12






Once you get your data in the format you wanted (using create_map) try using .write.text(json_path) to write the string to a file. Something like this: df.select("NEW_COLUMN_NAME").write.text(json_path)

– pault
Mar 25 at 19:12














when i try to write.text it says "Text data source does not support map" and if i try to Cast('string') it changes from "1": "VARIABLE_1": 1 to [1-> [VARIABLE_1-> 0]]

– TNS
Mar 25 at 19:59






when i try to write.text it says "Text data source does not support map" and if i try to Cast('string') it changes from "1": "VARIABLE_1": 1 to [1-> [VARIABLE_1-> 0]]

– TNS
Mar 25 at 19:59














My spark is version 2.4

– TNS
Mar 25 at 20:24





My spark is version 2.4

– TNS
Mar 25 at 20:24













I'm not able to test on 2.4 but does to_json work on the MapType() column? df.select(to_json("NEW_COLUMN_NAME")).write.text(json_path). If not, it's a little hacky but you can build the string manually: df.select(concat(lit('"'), "ID", lit('":"VARIABLE_1":"'), "VARIABLE_1", lit('"')).write.text(json_path)

– pault
Mar 25 at 20:56





I'm not able to test on 2.4 but does to_json work on the MapType() column? df.select(to_json("NEW_COLUMN_NAME")).write.text(json_path). If not, it's a little hacky but you can build the string manually: df.select(concat(lit('"'), "ID", lit('":"VARIABLE_1":"'), "VARIABLE_1", lit('"')).write.text(json_path)

– pault
Mar 25 at 20:56












1 Answer
1






active

oldest

votes


















0














How i fixed the issue:



-Used collect() to collect the create_map dataframe



-Used for to make a dictionary with every row of the dataframe



-Used json.dumps with the dictionary



-Saved using open(path, "w")






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%2f55344249%2fhow-to-save-a-json-from-dataframe-with-specific-format%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









    0














    How i fixed the issue:



    -Used collect() to collect the create_map dataframe



    -Used for to make a dictionary with every row of the dataframe



    -Used json.dumps with the dictionary



    -Saved using open(path, "w")






    share|improve this answer



























      0














      How i fixed the issue:



      -Used collect() to collect the create_map dataframe



      -Used for to make a dictionary with every row of the dataframe



      -Used json.dumps with the dictionary



      -Saved using open(path, "w")






      share|improve this answer

























        0












        0








        0







        How i fixed the issue:



        -Used collect() to collect the create_map dataframe



        -Used for to make a dictionary with every row of the dataframe



        -Used json.dumps with the dictionary



        -Saved using open(path, "w")






        share|improve this answer













        How i fixed the issue:



        -Used collect() to collect the create_map dataframe



        -Used for to make a dictionary with every row of the dataframe



        -Used json.dumps with the dictionary



        -Saved using open(path, "w")







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 18:56









        TNSTNS

        112 bronze badges




        112 bronze badges


















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            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%2f55344249%2fhow-to-save-a-json-from-dataframe-with-specific-format%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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript