Json incorporates the split problem

when the data is returned by the background, the two json are merged.
format such as:

 var str = "{"employees":[{"firstName":"Bill","lastName":"Gates"},{"firstName":"George","lastName":"Bush"},{"firstName":"Thomas","lastName":"Carter"}]}{"employees":[{"firstName":"Thomas","lastName":"Carter"}]}"
 
 

how do I split it?

Mar.10,2021

case by case
if the data returned by the backend is determined to be two and both json are returned with only one employees attribute:
use the lastIndexOf of string to find the split point

const i = str.lastIndexOf('{"employees":')
const result = [JSON.parse(str.substr(0, i)), JSON.parse(str.substr(i))]
console.log(result)

make sure they are all object and seamless

const arr = str.split('}{');
arr.length > 1 && arr.forEach((item, i) => {
  if (i === 0) {
    item += '}';
    console.log(JSON.parse(item));
  } else if (i === arr.length - 1) {
    item = '{' + item;
    console.log(JSON.parse(item));
  } else {
    item = `{${item}}`;
    console.log(JSON.parse(item));
  }
});
Menu