output = "{\"response\":200,\"message\":\"Id: 123 updated successfully\"}{\"response\":403,\"message\":\"Receipt with Id: 124 not updated\"}"
How to access the response on the above output? Unable to convert to json due to invalid format
The output you have is almost valid, you could put the two objects into an array and walk through them to check for the key value 'response' and for example print the values like so:
output = [{"response":200,"message":"Id: 123 updated successfully"},
{"response":403,"message":"Receipt with Id: 124 not updated"}];
for(let i=0; i<output.length; i++){
if(output[i].hasOwnProperty('response')){
console.log(output[i]['response']);
}
}
Output:
200
403
Please note the '[]' brackets put around the response objects returned to make the variable 'output' iterable.
try the following:
output = `{"response":200,"message":"Id: 123 updated successfully"}{"response":403,"message":"Receipt with Id: 124 not updated"}`
output = output.replaceAll("}", "},");
output = output.substring(0, output.length - 1);
output = "[" + output + "]"
console.log(output)
console.log(JSON.parse(output) )