Hariprasath
Hariprasath

Reputation: 539

Convert String to an Array in JS

In my project I have a use case like the below:

I have a response Array like below,

(4) [{…}, {…}, {…}, {…}]
0:{header: 0, name: "Name", field: "Id"}
1:{header: 3, name: "LastName", field: "Agreement__c"}
2:{header: 3, name: "LastName", field: "Amount__c"}
3:{header: 3, name: "LastName", field: "BIC__c"}
length:4

from the above I convert the above array to String by using,

JSON.stringify(responseArray) and store it in a string field.

After that I want to do some manipulation dynamically to that value of that field. So when I get the value back from the field it came as like below,

[{"header":0,"name":"Name","field":"Id"}, 
{"header":3,"name":"LastName","field":"Agreement__c"}, 
{"header":3,"name":"LastName","field":"Amount__c"}, 
{"header":3,"name":"LastName","field":"BIC__c"}]

Anyone please help me to convert the above string response to an Array in Javascript like as follows,

index 0 -> {"header":0,"name":"Name","field":"Id"}
index 1 -> {"header":3,"name":"LastName","field":"Agreement"}

I have tried with the split function but couldn't able to achieve the exact need.

Upvotes: 1

Views: 172

Answers (5)

Hariprasath
Hariprasath

Reputation: 539

yes, JSON.parse is the real easy answer for this.

Upvotes: 1

try this

    var textstr = '[{"header":0,"name":"Name","field":"Id"},{"header":3,"name":"LastName","field":"Agreement"}, {"header":3,"name":"LastName","field":"Amount"}, {"header":3,"name":"LastName","field":"BIC"}]';
    var textstr2 = JSON.parse(textstr);
    console.log(textstr2)
 

Upvotes: 0

Ranielle Canlas
Ranielle Canlas

Reputation: 634

You just need some basic string manipulations, By the way, I changed your string into a valid syntax

var str="{\"header\":0,\"name\":\"Name\",\"field\":\"Id\"},{\"header\":3,\"name\":\"LastName\",\"field\":\"Agreement\"},{\"header\":3,\"name\":\"LastName\",\"field\":\"Amount\"},{\"header\":3,\"name\":\"LastName\",\"field\":\"BIC\"}";

str=str.replace(/},{/g,"}|{");
var arr = str.split("|");
var json = [];
for(i=0; i<arr.length; i++){
json.push(JSON.parse(arr[i]));
}

//console.log(json);
console.log(json[0]);
console.log(json[1]);

Upvotes: 0

Ahmed Yousif
Ahmed Yousif

Reputation: 2348

you can use following code sample first append "[" at begging of your string and "]" at end of your string so your string will be well formatted as JSON array then it is so easy to parse it using JSON.parse built in function

 a = '['+'{"header":0,"name":"Name","field":"Id"}, {"header":3,"name":"LastName","field":"Agreement"}, {"header":3,"name":"LastName","field":"Amount"}, {"header":3,"name":"LastName","field":"BIC"}'+"]"

 var myarray = JSON.parse(a);

Upvotes: 2

Ray Toal
Ray Toal

Reputation: 88378

Put square brackets at the beginning and end of your string and call JSON.parse:

 $ node

 > const text = `{"header":0,"name":"Name","field":"Id"}, 
    {"header":3,"name":"LastName","field":"Agreement"}, 
    {"header":3,"name":"LastName","field":"Amount"}, 
    {"header":3,"name":"LastName","field":"BIC"}`

 > JSON.parse(`[${text}]`)
 [ { header: 0, name: 'Name', field: 'Id' },
   { header: 3, name: 'LastName', field: 'Agreement' },
   { header: 3, name: 'LastName', field: 'Amount' },
   { header: 3, name: 'LastName', field: 'BIC' } ]

Upvotes: 4

Related Questions