Adding quotations to stringified JSON object values










0















My stringified object data doesn't include quotation marks around object values, which errors when attempting JSON.parse():



' "affiliation": CORPORATE, "userId": 75c35d1c-5d12-4485-8fa8-b2f1551a3e6e '


I need the string to be:



' "affiliation": "CORPORATE", "userId": "75c35d1c-5d12-4485-8fa8-b2f1551a3e6e" '


I'm using this regex to add quotes to the object keys:



var newStr = str.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/g, '"$2": ');


For instance:
' affiliation: CORPORATE ' to ' "affiliation": CORPORATE '



There are only string values in my data, so I don't need to check value types. How can I alter my regex expression to add quotations to object values as well?










share|improve this question
























  • @ACD my regex currently adds quotes to the object key, but doesn't add quotes to the object value

    – alyx
    Nov 16 '18 at 4:01











  • Is it really impossible to fix the other side that generates it incorrectly?

    – zerkms
    Nov 16 '18 at 4:03











  • @zerkms Yes, this data is coming from a neo4j dump converted to CVS but the original source doesn't wrap quotes

    – alyx
    Nov 16 '18 at 4:04











  • is there a guarantee that special values like spaces, , : or " will not be present in the stringified object? If not, how would you add quotes to a key: a value, maybe key: value: 2?

    – lucascaro
    Nov 16 '18 at 4:24















0















My stringified object data doesn't include quotation marks around object values, which errors when attempting JSON.parse():



' "affiliation": CORPORATE, "userId": 75c35d1c-5d12-4485-8fa8-b2f1551a3e6e '


I need the string to be:



' "affiliation": "CORPORATE", "userId": "75c35d1c-5d12-4485-8fa8-b2f1551a3e6e" '


I'm using this regex to add quotes to the object keys:



var newStr = str.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/g, '"$2": ');


For instance:
' affiliation: CORPORATE ' to ' "affiliation": CORPORATE '



There are only string values in my data, so I don't need to check value types. How can I alter my regex expression to add quotations to object values as well?










share|improve this question
























  • @ACD my regex currently adds quotes to the object key, but doesn't add quotes to the object value

    – alyx
    Nov 16 '18 at 4:01











  • Is it really impossible to fix the other side that generates it incorrectly?

    – zerkms
    Nov 16 '18 at 4:03











  • @zerkms Yes, this data is coming from a neo4j dump converted to CVS but the original source doesn't wrap quotes

    – alyx
    Nov 16 '18 at 4:04











  • is there a guarantee that special values like spaces, , : or " will not be present in the stringified object? If not, how would you add quotes to a key: a value, maybe key: value: 2?

    – lucascaro
    Nov 16 '18 at 4:24













0












0








0








My stringified object data doesn't include quotation marks around object values, which errors when attempting JSON.parse():



' "affiliation": CORPORATE, "userId": 75c35d1c-5d12-4485-8fa8-b2f1551a3e6e '


I need the string to be:



' "affiliation": "CORPORATE", "userId": "75c35d1c-5d12-4485-8fa8-b2f1551a3e6e" '


I'm using this regex to add quotes to the object keys:



var newStr = str.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/g, '"$2": ');


For instance:
' affiliation: CORPORATE ' to ' "affiliation": CORPORATE '



There are only string values in my data, so I don't need to check value types. How can I alter my regex expression to add quotations to object values as well?










share|improve this question
















My stringified object data doesn't include quotation marks around object values, which errors when attempting JSON.parse():



' "affiliation": CORPORATE, "userId": 75c35d1c-5d12-4485-8fa8-b2f1551a3e6e '


I need the string to be:



' "affiliation": "CORPORATE", "userId": "75c35d1c-5d12-4485-8fa8-b2f1551a3e6e" '


I'm using this regex to add quotes to the object keys:



var newStr = str.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/g, '"$2": ');


For instance:
' affiliation: CORPORATE ' to ' "affiliation": CORPORATE '



There are only string values in my data, so I don't need to check value types. How can I alter my regex expression to add quotations to object values as well?







javascript regex replace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 4:00







alyx

















asked Nov 16 '18 at 3:55









alyxalyx

1,14662549




1,14662549












  • @ACD my regex currently adds quotes to the object key, but doesn't add quotes to the object value

    – alyx
    Nov 16 '18 at 4:01











  • Is it really impossible to fix the other side that generates it incorrectly?

    – zerkms
    Nov 16 '18 at 4:03











  • @zerkms Yes, this data is coming from a neo4j dump converted to CVS but the original source doesn't wrap quotes

    – alyx
    Nov 16 '18 at 4:04











  • is there a guarantee that special values like spaces, , : or " will not be present in the stringified object? If not, how would you add quotes to a key: a value, maybe key: value: 2?

    – lucascaro
    Nov 16 '18 at 4:24

















  • @ACD my regex currently adds quotes to the object key, but doesn't add quotes to the object value

    – alyx
    Nov 16 '18 at 4:01











  • Is it really impossible to fix the other side that generates it incorrectly?

    – zerkms
    Nov 16 '18 at 4:03











  • @zerkms Yes, this data is coming from a neo4j dump converted to CVS but the original source doesn't wrap quotes

    – alyx
    Nov 16 '18 at 4:04











  • is there a guarantee that special values like spaces, , : or " will not be present in the stringified object? If not, how would you add quotes to a key: a value, maybe key: value: 2?

    – lucascaro
    Nov 16 '18 at 4:24
















@ACD my regex currently adds quotes to the object key, but doesn't add quotes to the object value

– alyx
Nov 16 '18 at 4:01





@ACD my regex currently adds quotes to the object key, but doesn't add quotes to the object value

– alyx
Nov 16 '18 at 4:01













Is it really impossible to fix the other side that generates it incorrectly?

– zerkms
Nov 16 '18 at 4:03





Is it really impossible to fix the other side that generates it incorrectly?

– zerkms
Nov 16 '18 at 4:03













@zerkms Yes, this data is coming from a neo4j dump converted to CVS but the original source doesn't wrap quotes

– alyx
Nov 16 '18 at 4:04





@zerkms Yes, this data is coming from a neo4j dump converted to CVS but the original source doesn't wrap quotes

– alyx
Nov 16 '18 at 4:04













is there a guarantee that special values like spaces, , : or " will not be present in the stringified object? If not, how would you add quotes to a key: a value, maybe key: value: 2?

– lucascaro
Nov 16 '18 at 4:24





is there a guarantee that special values like spaces, , : or " will not be present in the stringified object? If not, how would you add quotes to a key: a value, maybe key: value: 2?

– lucascaro
Nov 16 '18 at 4:24












1 Answer
1






active

oldest

votes


















2














You can use the following regex:



/[ ](?=b)|b(?=,|[ ])/g


It starts matching a Space and use a look ahead for a Word boundary. It then uses an alternation for a Word boundary followed by a comma or a Space.



It uses the 'global' flag to match all.



Then you replace the matches with a double quote.






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%2f53331208%2fadding-quotations-to-stringified-json-object-values%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









    2














    You can use the following regex:



    /[ ](?=b)|b(?=,|[ ])/g


    It starts matching a Space and use a look ahead for a Word boundary. It then uses an alternation for a Word boundary followed by a comma or a Space.



    It uses the 'global' flag to match all.



    Then you replace the matches with a double quote.






    share|improve this answer





























      2














      You can use the following regex:



      /[ ](?=b)|b(?=,|[ ])/g


      It starts matching a Space and use a look ahead for a Word boundary. It then uses an alternation for a Word boundary followed by a comma or a Space.



      It uses the 'global' flag to match all.



      Then you replace the matches with a double quote.






      share|improve this answer



























        2












        2








        2







        You can use the following regex:



        /[ ](?=b)|b(?=,|[ ])/g


        It starts matching a Space and use a look ahead for a Word boundary. It then uses an alternation for a Word boundary followed by a comma or a Space.



        It uses the 'global' flag to match all.



        Then you replace the matches with a double quote.






        share|improve this answer















        You can use the following regex:



        /[ ](?=b)|b(?=,|[ ])/g


        It starts matching a Space and use a look ahead for a Word boundary. It then uses an alternation for a Word boundary followed by a comma or a Space.



        It uses the 'global' flag to match all.



        Then you replace the matches with a double quote.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 4:40

























        answered Nov 16 '18 at 4:32









        Poul BakPoul Bak

        5,48831233




        5,48831233





























            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%2f53331208%2fadding-quotations-to-stringified-json-object-values%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

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線