I want to take “label” from json response. Tried res.json().data.label but not worked
{
"data": [
{
"id": "test",
"label": "PAK",
"labelFormat": "[SE]-[IDX]",
}
],
"count": 5
}
I want to take “label” from json response. Tried res.json().data.label but not worked
{
"data": [
{
"id": "test",
"label": "PAK",
"labelFormat": "[SE]-[IDX]",
}
],
"count": 5
}
Not a valid json response, the comma after the labelformat is invalid.
this code works for me.
export default function () {
const Payload = JSON.stringify({
"data": [
{
"id": "test",
"label": "PAK",
"labelFormat": "[SE]-[IDX]"
}
],
"count": 5
});
console.log(Payload);
const ResponseJson = JSON.parse(Payload);
console.log(ResponseJson.data[0].label);
}
INFO[0000] {"data":[{"id":"test","label":"PAK","labelFormat":"[SE]-[IDX]"}],"count":5} source=console
INFO[0000] PAK source=console
Data is an array, so first you need to specify the index of the element you want to extract. In your case it would be index 0. So res.json().data[0].label
.