skip to Main Content

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}]

2

Answers


  1. 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);
    Login or Signup to reply.
  2. 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;
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search