简体   繁体   中英

javascript - convert string to json array

I was using s3 select to fetch selective data and display them on my front end. I converted array of byte to buffer and then to string like below as string

let dataString = Buffer.concat(records).toString('utf8');

the result i got was string like below

 {"id":"1","obj1":"191.25","obj2":"11.81","obj3":"3.44","obj4":"15.62"}
 {"id":"2","obj1":"642.00","obj2":"4.33","obj3":"0.00","obj4":"11.33"}
 {"id":"3","obj1":"153.76","obj2":"94.77","obj3":"16.80","obj4":"29.79"}
 {"id":"4","obj1":"61.71","obj2":"0.43","obj3":"0.00","obj4":"8.14"}
 {"id":"5","obj1":"194.33","obj2":"108.89","obj3":"14.13","obj4":"168.60"}
 {"id":"6","obj1":"204.31","obj2":"137.41","obj3":"34.76","obj4":"193.16"}
 {"id":"7","obj1":"199.53","obj2":"34.53","obj3":"16.29","obj4":"26.56"}
 {"id":"8","obj1":"77.33","obj2":"5.00","obj3":"12.50","obj4":"0.00"}
 {"id":"9","obj1":"128.54","obj2":"101.60","obj3":"15.76","obj4":"46.23"}
 {"id":"10","obj1":"107.00","obj2":"116.67","obj3":"34.42","obj4":"8.75"}
 {"id":"12","obj1":"206.05","obj2":"155.03","obj3":"36.96","obj4":"148.99"}
 {"id":"13","obj1":"133.93","obj2":"142.79","obj3":"39.91","obj4":"98.30"}
  

Now i want to convert them to json array, i got a solution like below

let dataArray = dataString.split('\n');
//remove white spaces and commas etc
dataArray = dataArray.filter(d=> d.length >2);
//change string to json
dataArray = dataArray.map(d=> JSON.parse(d));

Now the problem is that i have splitted them with new line and wont work if the json is compressed or data itself can have new line.

What is the best way to handle this situation. i want the output like below

[{"id":"1","obj1":"191.25","obj2":"11.81","obj3":"3.44","obj4":"15.62"},
 {"id":"2","obj1":"642.00","obj2":"4.33","obj3":"0.00","obj4":"11.33"},
 {"id":"3","obj1":"153.76","obj2":"94.77","obj3":"16.80","obj4":"29.79"},
 {"id":"4","obj1":"61.71","obj2":"0.43","obj3":"0.00","obj4":"8.14"},
 {"id":"5","obj1":"194.33","obj2":"108.89","obj3":"14.13","obj4":"168.60"},
 {"id":"6","obj1":"204.31","obj2":"137.41","obj3":"34.76","obj4":"193.16"},
 {"id":"7","obj1":"199.53","obj2":"34.53","obj3":"16.29","obj4":"26.56"},
 {"id":"8","obj1":"77.33","obj2":"5.00","obj3":"12.50","obj4":"0.00"},
 {"id":"9","obj1":"128.54","obj2":"101.60","obj3":"15.76","obj4":"46.23"},
 {"id":"10","obj1":"107.00","obj2":"116.67","obj3":"34.42","obj4":"8.75"},
 {"id":"12","obj1":"206.05","obj2":"155.03","obj3":"36.96","obj4":"148.99"},
 {"id":"13","obj1":"133.93","obj2":"142.79","obj3":"39.91","obj4":"98.30"}
]

Yeah, it is not a very good idea to concatenate objects into a string like that. If you don't have any other choice, however, something like that should do the trick:

const initialString = `{"id":"1","obj1":"191.25","obj2":"11.81","obj3":"3.44","obj4":"15.62"}
 {"id":"2","obj1":"642.00","obj2":"4.33","obj3":"0.00","obj4":"11.33"}
 {"id":"3","obj1":"153.76","obj2":"94.77","obj3":"16.80","obj4":"29.79"}
 {"id":"4","obj1":"61.71","obj2":"0.43","obj3":"0.00","obj4":"8.14"}
 {"id":"5","obj1":"194.33","obj2":"108.89","obj3":"14.13","obj4":"168.60"}
 {"id":"6","obj1":"204.31","obj2":"137.41","obj3":"34.76","obj4":"193.16"}
 {"id":"7","obj1":"199.53","obj2":"34.53","obj3":"16.29","obj4":"26.56"}
 {"id":"8","obj1":"77.33","obj2":"5.00","obj3":"12.50","obj4":"0.00"}
 {"id":"9","obj1":"128.54","obj2":"101.60","obj3":"15.76","obj4":"46.23"}
 {"id":"10","obj1":"107.00","obj2":"116.67","obj3":"34.42","obj4":"8.75"}
 {"id":"12","obj1":"206.05","obj2":"155.03","obj3":"36.96","obj4":"148.99"}
 {"id":"13","obj1":"133.93","obj2":"142.79","obj3":"39.91","obj4":"98.30"}`;
 
const json = `[${initialString.replace(/}\s*{/g, '},{')}]`;

const array = JSON.parse(json);

@sumit please take a look at this solution.

 let dataString=`{"id":"1","obj1":"191.25","obj2":"11.81","obj3":"3.44","obj4":"15.62"} {"id":"2","obj1":"642.00","obj2":"4.33","obj3":"0.00","obj4":"11.33"} {"id":"3","obj1":"153.76","obj2":"94.77","obj3":"16.80","obj4":"29.79"} {"id":"4","obj1":"61.71","obj2":"0.43","obj3":"0.00","obj4":"8.14"} {"id":"5","obj1":"194.33","obj2":"108.89","obj3":"14.13","obj4":"168.60"} {"id":"6","obj1":"204.31","obj2":"137.41","obj3":"34.76","obj4":"193.16"} {"id":"7","obj1":"199.53","obj2":"34.53","obj3":"16.29","obj4":"26.56"} {"id":"8","obj1":"77.33","obj2":"5.00","obj3":"12.50","obj4":"0.00"} {"id":"9","obj1":"128.54","obj2":"101.60","obj3":"15.76","obj4":"46.23"} {"id":"10","obj1":"107.00","obj2":"116.67","obj3":"34.42","obj4":"8.75"} {"id":"12","obj1":"206.05","obj2":"155.03","obj3":"36.96","obj4":"148.99"} {"id":"13","obj1":"133.93","obj2":"142.79","obj3":"39.91","obj4":"98.30"}`; let dataArray = dataString.match(/{(?:[^{}]*|(R))*}/g); dataArray = dataArray.map(d=> JSON.parse(d)); console.log(dataArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM