Home > Software engineering >  Convert string array of objects to json in react native
Convert string array of objects to json in react native

Time:01-14

I have a string array of objects coming to a react native frontend. I'm not sure how to convert this to a json to read it in front end. This is the string I need to convert:

[{_id=63c1400ae18737cd3c67d8d4, user_id=1185.0, device_serial=544008293, site_id=2000000.0, datetime=2023-01-13T20:26:54.150Z, timezone=Asia/Seoul, code=11820, offset=3.24E7, __v=0.0, device_name=544008293, user_name=sanj, user_key=7722352345}, {_id=63c12467e18737cd3c67d809, user_id=1185.0, device_serial=544008293, site_id=2000000.0, datetime=2023-01-13T15:03:53.000Z, timezone=Asia/Seoul, code=10080, offset=3.24E7, __v=0.0, device_name=544008293, user_name=sanj, user_key=7722352345}]

CodePudding user response:

let stringArr =
  '[{_id=63c1400ae18737cd3c67d8d4, user_id=1185.0, device_serial=544008293, site_id=2000000.0, datetime=2023-01-13T20:26:54.150Z, timezone=Asia/Seoul, code=11820, offset=3.24E7, __v=0.0, device_name=544008293, user_name=sanj, user_key=7722352345}, {_id=63c12467e18737cd3c67d809, user_id=1185.0, device_serial=544008293, site_id=2000000.0, datetime=2023-01-13T15:03:53.000Z, timezone=Asia/Seoul, code=10080, offset=3.24E7, __v=0.0, device_name=544008293, user_name=sanj, user_key=7722352345}]';
  
console.log("given array -> ",stringArr)

stringArr = stringArr.replace('[', '').replace(']', '').slice(1).slice(0, -1).split(',');

stringArr = stringArr.map(e => {
  const trimString = e.trim();
  const equalSpilt = trimString.split('=');
  const key = equalSpilt[0];
  const obj = {};
  obj[key] = equalSpilt[1];
  return obj;
});

console.log("final output -> ",stringArr)

possible if you can convert your array to string

let stringArr =
  '[{_id=63c1400ae18737cd3c67d8d4, user_id=1185.0, device_serial=544008293, site_id=2000000.0, datetime=2023-01-13T20:26:54.150Z, timezone=Asia/Seoul, code=11820, offset=3.24E7, __v=0.0, device_name=544008293, user_name=sanj, user_key=7722352345}, {_id=63c12467e18737cd3c67d809, user_id=1185.0, device_serial=544008293, site_id=2000000.0, datetime=2023-01-13T15:03:53.000Z, timezone=Asia/Seoul, code=10080, offset=3.24E7, __v=0.0, device_name=544008293, user_name=sanj, user_key=7722352345}]';

stringArr = stringArr.replace('[', '').replace(']', '').slice(1).slice(0, -1).split(',');

stringArr = stringArr.map(e => {
  const trimString = e.trim();
  const equalSpilt = trimString.split('=');
  const key = equalSpilt[0];
  const obj = {};
  obj[key] = equalSpilt[1];
  return obj;
});

CodePudding user response:

You can achieve it like this:

let strval =
  "[{_id=63c1400ae18737cd3c67d8d4, user_id=1185.0, device_serial=544008293, site_id=2000000.0, datetime=2023-01-13T20:26:54.150Z, timezone=Asia/Seoul, code=11820, offset=3.24E7, __v=0.0, device_name=544008293, user_name=sanj, user_key=7722352345}, {_id=63c12467e18737cd3c67d809, user_id=1185.0, device_serial=544008293, site_id=2000000.0, datetime=2023-01-13T15:03:53.000Z, timezone=Asia/Seoul, code=10080, offset=3.24E7, __v=0.0, device_name=544008293, user_name=sanj, user_key=7722352345}]";

strval = strval.replace("[", "").replace("]", "");

const splittedObj = strval.split("},");

let arr = [];

splittedObj.forEach((e, i) => {
  e = e.replace("{", "").replace("}", "");
  let obj = {};

  e.split(",").map((element) => {
    const splittedElement = element.trim().split("=");
    obj[splittedElement[0]] = splittedElement[1];
  });
  arr.push(obj);
});

console.log(arr);

  • Related