1

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.

halfer
20.2k20 gold badges111 silver badges208 bronze badges
asked Aug 29, 2018 at 5:49
5
  • "{"header":0,"name":"Name","field":"Id"}, That is invalid syntax. Commented Aug 29, 2018 at 5:50
  • 1
    Probably the OP meant to use apostrophes or backticks to delimit the whole string Commented Aug 29, 2018 at 5:51
  • What do you mean by invalid syntax? Commented Aug 29, 2018 at 5:52
  • @CertainPerformance meant that you are delimiting the keys and values as well as the entire string with double quotes and you should have used single quotes and double quotes. Commented Aug 29, 2018 at 5:56
  • I have edited my question. I don't know why someone down vote to this question? I am new to JS. Commented Aug 29, 2018 at 6:22

5 Answers 5

4

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' } ]
answered Aug 29, 2018 at 5:54
Sign up to request clarification or add additional context in comments.

Comments

2

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);
answered Aug 29, 2018 at 5:58

2 Comments

While your code may correct as answer But Elaborating what your code does, It can improve the quality of your answer. Checkout the Article : How do I write a good answer?
@LuFFy I added some Elaborations
1

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

Sourabh Kumar Sharma
2,8273 gold badges27 silver badges33 bronze badges
answered Aug 29, 2018 at 6:28

Comments

0

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]);

LuFFy
9,38310 gold badges43 silver badges59 bronze badges
answered Aug 29, 2018 at 6:01

Comments

0

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)
 

LuFFy
9,38310 gold badges43 silver badges59 bronze badges
answered Aug 29, 2018 at 6:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.