skip to Main Content

I have below json string & want to create new Json Array from this data

Raw Data:

[
    ["yrxqBHmPkNhZ60_eab97ebf-c2a3-40a5-972a-91597ad9a4ca_99371", "SUCCEEDED", "2023-08-31T21:59:31.325000+05:30", "2023-08-31T22:13:42.165000+05:30"],
    ["yrxqBHmPkNhZ60_f42702be-16c3-44bf-bc3b-6719c740a5e3_98405", "SUCCEEDED", "2023-08-31T21:43:25.581000+05:30", "2023-08-31T21:57:47.681000+05:30"],
    ["rk9QMf81WrT7DO_edd6e8de-9de4-4885-b970-9311443d81d5_98750", "SUCCEEDED", "2023-08-31T21:49:10.578000+05:30", "2023-08-31T21:56:20.197000+05:30"]
]

I have tried below code to convert this into expected

var dataObj = JSON.parse(myStr); // here myStr is above data string

var newData = new Array();

for(var i in dataObj) {  

 newData.push(dataObj[i]);

}

data.push(newData);

Expected Result:

[
    "type": "buffer",
    "data":[
    ["yrxqBHmPkNhZ60_eab97ebf-c2a3-40a5-972a-91597ad9a4ca_99371", "SUCCEEDED", "2023-08-31T21:59:31.325000+05:30", "2023-08-31T22:13:42.165000+05:30"],
    ["yrxqBHmPkNhZ60_f42702be-16c3-44bf-bc3b-6719c740a5e3_98405", "SUCCEEDED", "2023-08-31T21:43:25.581000+05:30", "2023-08-31T21:57:47.681000+05:30"],
    ["rk9QMf81WrT7DO_edd6e8de-9de4-4885-b970-9311443d81d5_98750", "SUCCEEDED", "2023-08-31T21:49:10.578000+05:30", "2023-08-31T21:56:20.197000+05:30"]
    ]
]

3

Answers


  1. You don’t need to loop your JSON array, just assign it directly to data field:

    var dataObj = [
        ["yrxqBHmPkNhZ60_eab97ebf-c2a3-40a5-972a-91597ad9a4ca_99371", "SUCCEEDED", "2023-08-31T21:59:31.325000+05:30", "2023-08-31T22:13:42.165000+05:30"],
        ["yrxqBHmPkNhZ60_f42702be-16c3-44bf-bc3b-6719c740a5e3_98405", "SUCCEEDED", "2023-08-31T21:43:25.581000+05:30", "2023-08-31T21:57:47.681000+05:30"],
        ["rk9QMf81WrT7DO_edd6e8de-9de4-4885-b970-9311443d81d5_98750", "SUCCEEDED", "2023-08-31T21:49:10.578000+05:30", "2023-08-31T21:56:20.197000+05:30"]
    ];
    
    var data = {type: 'buffer', data: []};
    
    data.data = dataObj;
    
    console.log(data);
    Login or Signup to reply.
  2. To get the result you want, you follow the below steps.

    1. Parse the JSON array to the Javascript array
    2. Create a new object with type, and actual data.
    const myStr = '[["yrxqBHmPkNhZ60_eab97ebf-c2a3-40a5-972a-91597ad9a4ca_99371","SUCCEEDED","2023-08-31T21:59:31.325000+05:30","2023-08-31T22:13:42.165000+05:30"],["yrxqBHmPkNhZ60_f42702be-16c3-44bf-bc3b-6719c740a5e3_98405","SUCCEEDED","2023-08-31T21:43:25.581000+05:30","2023-08-31T21:57:47.681000+05:30"],["rk9QMf81WrT7DO_edd6e8de-9de4-4885-b970-9311443d81d5_98750","SUCCEEDED","2023-08-31T21:49:10.578000+05:30","2023-08-31T21:56:20.197000+05:30"]]';
    
    const jsonData = JSON.parse(myStr); // here myStr is JSON data string
    const data = {
      type: "buffer",
      data: jsonData,
    };
    
    console.log(data);
    Login or Signup to reply.
  3. Here is working example:

    let obj = [
        ["yrxqBHmPkNhZ60_eab97ebf-c2a3-40a5-972a-91597ad9a4ca_99371", "SUCCEEDED", "2023-08-31T21:59:31.325000+05:30", "2023-08-31T22:13:42.165000+05:30"],
        ["yrxqBHmPkNhZ60_f42702be-16c3-44bf-bc3b-6719c740a5e3_98405", "SUCCEEDED", "2023-08-31T21:43:25.581000+05:30", "2023-08-31T21:57:47.681000+05:30"],
        ["rk9QMf81WrT7DO_edd6e8de-9de4-4885-b970-9311443d81d5_98750", "SUCCEEDED", "2023-08-31T21:49:10.578000+05:30", "2023-08-31T21:56:20.197000+05:30"]
    ]
    
    
    console.log(Object.assign({type:"buffer"},{data : obj}))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search