I need to convert my JSON response to an object, how can I achieve this?
My JSON response:
[
{
"id":296,
"nama":"Appetizer"
},
{
"id":295,
"nama":"Bahan"
}
]
Provided your response is a valid JSON, just do this
var obj = JSON.parse(response);
You need to use JSON.parse in a try catch to catch errors if JSON is not valid.
let str = '[{"id":296,"nama":"Appetizer"},{"id":295,"nama":"Bahan"}]';
try {
let obj = JSON.parse(str);
} catch (ex) {
console.error(ex);
}
To convert back your object to string, use Stringify
JSON.stringify(obj)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With