-3
{ data1: "'opt01', 'opt\'02', 'op,t03'", data2: "plaintext"}

I have the above returned json data.

How can I obtain data1 by an array format so that I can loop the data inside one by one?

HUNG
  • 525
  • 7
  • 17
  • 2
    Have you considered trying `split`? – CertainPerformance Jul 27 '18 at 20:22
  • @CertainPerformance but I worry if the data inside may sometimes also contains a comma – HUNG Jul 27 '18 at 20:23
  • 1
    Can there be a *nested* '? Are the 's always present and balanced? (If it were me, I'd try to get the data *as* a JSON array to begin with; this avoids so much complexity.) – user2864740 Jul 27 '18 at 20:24
  • 1
    Do you have any control over the source of the data? The best solution would be to send it in a better format to begin with. – John Montgomery Jul 27 '18 at 20:25
  • 1
    Given the constraints of ' mentioned above (ie. cannot be nested [or escaped] and always present/balanced), https://stackoverflow.com/q/19913667/2864740 would work. – user2864740 Jul 27 '18 at 20:30
  • 1
    Replace the single quotes with double quotes, wrap it in square brackets, and you've got a JSON-parsable string: `data1 = "[" + data1.replace(/'/g, "\"") + "]"; var array = JSON.parse(data1));` – Tyler Roper Jul 27 '18 at 20:32
  • @user2864740 I'm not too good with regex, will that work with spaces after the commas as in the example here? – John Montgomery Jul 27 '18 at 20:32
  • 1
    @JohnMontgomery The question/answer linked above would extract any sequence of "strings" within single quotes, regardless of what is in-between: `'a','b'`,`'a''b'`,`'a'foo--bar'b'`. However, it would fail for `''Hello!', shouted Sam', 'dodo'` and any form violating the simple assumptions stated. – user2864740 Jul 27 '18 at 20:33
  • Since you've changed the question title, you have to escape `'` in `"'opt01', 'opt\'02', 'op,t03'"` twice as it's quoted twice. and look into my answer =)) – Kosh Jul 27 '18 at 20:56

4 Answers4

1

If each string in data1 is single quote delimited

var data1 = jsonData.data1;
data1 = data1.substring(1).slice(-1); // get rid of the leading and trailing '
var strings = data1.split("', '"); // split on the actual delimiter, not just the ,.
bcr666
  • 2,157
  • 1
  • 12
  • 23
1

If you wrapped the string in square brackets and replaced the single-quotes with double-quotes, you'd have a valid JSON string. At that point, JSON.parse() would turn that into an actual array.

var obj = { data1: "'opt1', 'opt2', 'opt3'", data2: "plaintext"};

var strData = "[" + obj.data1.replace(/'/g, "\"") + "]";
var arrData = JSON.parse(strData);
console.log(arrData);

Or using template literals...

var obj = { data1: "'opt1', 'opt2', 'opt3'", data2: "plaintext"};

var strData = `[${obj.data1.replace(/'/g, "\"")}]`;
var arrData = JSON.parse(strData);
console.log(arrData);

The only reason I suggest this over the standard split("', '") is in the event your data comes back without spaces in it (data1: "'opt1','opt2','opt3'"), the parse would still work, whereas split would not (unless you did a similar replace to that above and removed the spaces to be safe).

That said, if this isn't a possible scenario, either method would work.

Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
1

const obj = {data1: "'opt1', 'opt2', 'opt3'", data2: "plaintext"}    
const array = obj.data1.replace(/'/g,'').split(', ')
console.log(array)
Lobster Fighter
  • 718
  • 6
  • 7
1

If your datasource is reliable, you might use eval.
If not reliable, check the data first.

var d = { data1: "'opt1', 'op,t2', 'op\\'t3'", data2: "plaintext"};

d.data1 = eval(`[${d.data1}]`);

console.log(d);

But the best solution would be to fix the datasource as @JohnMontgomery suggested.

Kosh
  • 16,966
  • 2
  • 19
  • 34
  • 1
    In this case above, you're using `eval()` to do exactly what a `JSON.parse()` could do. `eval` is slow (requires a recompile) and opens you up to some malicious attacks. If there is ever a way to avoid using `eval()`, do it! :) – Tyler Roper Jul 27 '18 at 20:44
  • @TylerRoper, thank you, I know what `JSON.parse` do. The difference in details. You have to replace quotes and can replace something what should not to. – Kosh Jul 27 '18 at 20:49
  • 1
    @HUNG since you accepted this answer: be **very** careful about using `eval` on external data, especially if you don't have control of the source yourself. It's probably the single easiest way to introduce a security vulnerability. – John Montgomery Jul 27 '18 at 21:24
  • 1
    I would *much rather* "do the extra steps" than use `eval`. Writing *correct, safe code* takes priority over shortcuts. (Protip: if the poo starts flying, it's 'not good' to have your name on questionable commits..) – user2864740 Jul 27 '18 at 21:26
  • Guys, there must be a `evalophobia` group... I do not suggest to `eval` everything around. But I suggested to **check data first**. Anyway thak you for your comments, they make sence! – Kosh Jul 27 '18 at 21:36
  • @TylerRoper, well, I'll cross my fingers! =)) – Kosh Jul 27 '18 at 21:38