Split a string into a list of strings












1















I have an input Json string like this:



var x= "[{"name":"ahmed","age":"26"}, 
{"name":"Sam","age":"25"}]"


I want to split it to a list of strings from{ to } as follows without removing a delimiter



var list;
list[0]= {"name":"ahmed","age":"26"}
list[1]= {"name":"Sam","age":"25"}


using the split method removes the delimiter and does not yield the correct format



x= x.replace(/[/]/g, '/'); //to remove [ and ]
x= x.replace( /},/ ,'}n' ); // does not split the string to list of strings
list = x; // type mismatch error since x is a string and list is array









share|improve this question


















  • 8





    How about parsing the JSON using JSON.parse?

    – Teemu
    Nov 13 '18 at 9:51











  • Possible duplicate of JS string.split() without removing the delimiters

    – pwolaq
    Nov 13 '18 at 9:57











  • @Teemu thanks alot.. don't know how this didn't cross my mind...

    – User MA
    Nov 13 '18 at 10:00
















1















I have an input Json string like this:



var x= "[{"name":"ahmed","age":"26"}, 
{"name":"Sam","age":"25"}]"


I want to split it to a list of strings from{ to } as follows without removing a delimiter



var list;
list[0]= {"name":"ahmed","age":"26"}
list[1]= {"name":"Sam","age":"25"}


using the split method removes the delimiter and does not yield the correct format



x= x.replace(/[/]/g, '/'); //to remove [ and ]
x= x.replace( /},/ ,'}n' ); // does not split the string to list of strings
list = x; // type mismatch error since x is a string and list is array









share|improve this question


















  • 8





    How about parsing the JSON using JSON.parse?

    – Teemu
    Nov 13 '18 at 9:51











  • Possible duplicate of JS string.split() without removing the delimiters

    – pwolaq
    Nov 13 '18 at 9:57











  • @Teemu thanks alot.. don't know how this didn't cross my mind...

    – User MA
    Nov 13 '18 at 10:00














1












1








1








I have an input Json string like this:



var x= "[{"name":"ahmed","age":"26"}, 
{"name":"Sam","age":"25"}]"


I want to split it to a list of strings from{ to } as follows without removing a delimiter



var list;
list[0]= {"name":"ahmed","age":"26"}
list[1]= {"name":"Sam","age":"25"}


using the split method removes the delimiter and does not yield the correct format



x= x.replace(/[/]/g, '/'); //to remove [ and ]
x= x.replace( /},/ ,'}n' ); // does not split the string to list of strings
list = x; // type mismatch error since x is a string and list is array









share|improve this question














I have an input Json string like this:



var x= "[{"name":"ahmed","age":"26"}, 
{"name":"Sam","age":"25"}]"


I want to split it to a list of strings from{ to } as follows without removing a delimiter



var list;
list[0]= {"name":"ahmed","age":"26"}
list[1]= {"name":"Sam","age":"25"}


using the split method removes the delimiter and does not yield the correct format



x= x.replace(/[/]/g, '/'); //to remove [ and ]
x= x.replace( /},/ ,'}n' ); // does not split the string to list of strings
list = x; // type mismatch error since x is a string and list is array






javascript string replace split substring






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 9:50









User MAUser MA

111




111








  • 8





    How about parsing the JSON using JSON.parse?

    – Teemu
    Nov 13 '18 at 9:51











  • Possible duplicate of JS string.split() without removing the delimiters

    – pwolaq
    Nov 13 '18 at 9:57











  • @Teemu thanks alot.. don't know how this didn't cross my mind...

    – User MA
    Nov 13 '18 at 10:00














  • 8





    How about parsing the JSON using JSON.parse?

    – Teemu
    Nov 13 '18 at 9:51











  • Possible duplicate of JS string.split() without removing the delimiters

    – pwolaq
    Nov 13 '18 at 9:57











  • @Teemu thanks alot.. don't know how this didn't cross my mind...

    – User MA
    Nov 13 '18 at 10:00








8




8





How about parsing the JSON using JSON.parse?

– Teemu
Nov 13 '18 at 9:51





How about parsing the JSON using JSON.parse?

– Teemu
Nov 13 '18 at 9:51













Possible duplicate of JS string.split() without removing the delimiters

– pwolaq
Nov 13 '18 at 9:57





Possible duplicate of JS string.split() without removing the delimiters

– pwolaq
Nov 13 '18 at 9:57













@Teemu thanks alot.. don't know how this didn't cross my mind...

– User MA
Nov 13 '18 at 10:00





@Teemu thanks alot.. don't know how this didn't cross my mind...

– User MA
Nov 13 '18 at 10:00












3 Answers
3






active

oldest

votes


















1














As commented above by Teemu, using JSON.parse is the safe and correct way to parse json.






const x = "[{"name":"ahmed","age":"26"},{"name":"Sam","age":"25"}]";

console.log(JSON.parse(x));








share|improve this answer
























  • thanks @David for clarification

    – User MA
    Nov 13 '18 at 10:01



















1














You can parse it to JSON first then use map to make list out of it.



var parsedX = JSON.parse(x);
var list = parsedX.map(x => JSON.stringify(x).replace(/"/g,'\"'));





share|improve this answer

































    0














    You could use following code to achieve desired result with this particular type of json:



    var str = "[{"name":"ahmed","age":"26"}, {"name":"Sam","age":"25"}]";
    var list = str.match(/[{][^{}]*[}]/gm);

    alert(list)


    Fell free to ask if you have any questions:






    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%2f53278184%2fsplit-a-string-into-a-list-of-strings%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









      1














      As commented above by Teemu, using JSON.parse is the safe and correct way to parse json.






      const x = "[{"name":"ahmed","age":"26"},{"name":"Sam","age":"25"}]";

      console.log(JSON.parse(x));








      share|improve this answer
























      • thanks @David for clarification

        – User MA
        Nov 13 '18 at 10:01
















      1














      As commented above by Teemu, using JSON.parse is the safe and correct way to parse json.






      const x = "[{"name":"ahmed","age":"26"},{"name":"Sam","age":"25"}]";

      console.log(JSON.parse(x));








      share|improve this answer
























      • thanks @David for clarification

        – User MA
        Nov 13 '18 at 10:01














      1












      1








      1







      As commented above by Teemu, using JSON.parse is the safe and correct way to parse json.






      const x = "[{"name":"ahmed","age":"26"},{"name":"Sam","age":"25"}]";

      console.log(JSON.parse(x));








      share|improve this answer













      As commented above by Teemu, using JSON.parse is the safe and correct way to parse json.






      const x = "[{"name":"ahmed","age":"26"},{"name":"Sam","age":"25"}]";

      console.log(JSON.parse(x));








      const x = "[{"name":"ahmed","age":"26"},{"name":"Sam","age":"25"}]";

      console.log(JSON.parse(x));





      const x = "[{"name":"ahmed","age":"26"},{"name":"Sam","age":"25"}]";

      console.log(JSON.parse(x));






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 13 '18 at 9:56









      David LemonDavid Lemon

      997618




      997618













      • thanks @David for clarification

        – User MA
        Nov 13 '18 at 10:01



















      • thanks @David for clarification

        – User MA
        Nov 13 '18 at 10:01

















      thanks @David for clarification

      – User MA
      Nov 13 '18 at 10:01





      thanks @David for clarification

      – User MA
      Nov 13 '18 at 10:01













      1














      You can parse it to JSON first then use map to make list out of it.



      var parsedX = JSON.parse(x);
      var list = parsedX.map(x => JSON.stringify(x).replace(/"/g,'\"'));





      share|improve this answer






























        1














        You can parse it to JSON first then use map to make list out of it.



        var parsedX = JSON.parse(x);
        var list = parsedX.map(x => JSON.stringify(x).replace(/"/g,'\"'));





        share|improve this answer




























          1












          1








          1







          You can parse it to JSON first then use map to make list out of it.



          var parsedX = JSON.parse(x);
          var list = parsedX.map(x => JSON.stringify(x).replace(/"/g,'\"'));





          share|improve this answer















          You can parse it to JSON first then use map to make list out of it.



          var parsedX = JSON.parse(x);
          var list = parsedX.map(x => JSON.stringify(x).replace(/"/g,'\"'));






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 10:15









          qiAlex

          2,0261724




          2,0261724










          answered Nov 13 '18 at 10:01









          ptdienptdien

          744




          744























              0














              You could use following code to achieve desired result with this particular type of json:



              var str = "[{"name":"ahmed","age":"26"}, {"name":"Sam","age":"25"}]";
              var list = str.match(/[{][^{}]*[}]/gm);

              alert(list)


              Fell free to ask if you have any questions:






              share|improve this answer




























                0














                You could use following code to achieve desired result with this particular type of json:



                var str = "[{"name":"ahmed","age":"26"}, {"name":"Sam","age":"25"}]";
                var list = str.match(/[{][^{}]*[}]/gm);

                alert(list)


                Fell free to ask if you have any questions:






                share|improve this answer


























                  0












                  0








                  0







                  You could use following code to achieve desired result with this particular type of json:



                  var str = "[{"name":"ahmed","age":"26"}, {"name":"Sam","age":"25"}]";
                  var list = str.match(/[{][^{}]*[}]/gm);

                  alert(list)


                  Fell free to ask if you have any questions:






                  share|improve this answer













                  You could use following code to achieve desired result with this particular type of json:



                  var str = "[{"name":"ahmed","age":"26"}, {"name":"Sam","age":"25"}]";
                  var list = str.match(/[{][^{}]*[}]/gm);

                  alert(list)


                  Fell free to ask if you have any questions:







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 10:34









                  Tornike ShavishviliTornike Shavishvili

                  61021022




                  61021022






























                      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%2f53278184%2fsplit-a-string-into-a-list-of-strings%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

                      Florida Star v. B. J. F.

                      Error while running script in elastic search , gateway timeout

                      Adding quotations to stringified JSON object values