Split a string into a list of strings
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
add a comment |
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
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
add a comment |
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
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
javascript string replace split substring
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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));
thanks @David for clarification
– User MA
Nov 13 '18 at 10:01
add a comment |
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,'\"'));
add a comment |
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:
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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));
thanks @David for clarification
– User MA
Nov 13 '18 at 10:01
add a comment |
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));
thanks @David for clarification
– User MA
Nov 13 '18 at 10:01
add a comment |
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));
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));
answered Nov 13 '18 at 9:56
David LemonDavid Lemon
997618
997618
thanks @David for clarification
– User MA
Nov 13 '18 at 10:01
add a comment |
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
add a comment |
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,'\"'));
add a comment |
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,'\"'));
add a comment |
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,'\"'));
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,'\"'));
edited Nov 13 '18 at 10:15
qiAlex
2,0261724
2,0261724
answered Nov 13 '18 at 10:01
ptdienptdien
744
744
add a comment |
add a comment |
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:
add a comment |
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:
add a comment |
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:
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:
answered Nov 13 '18 at 10:34
Tornike ShavishviliTornike Shavishvili
61021022
61021022
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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