0

Output from:

console.log(response);
console.log(response[0].group_id);

is as below:

[{"group_id":8}]
undefined

what is wrong here? How do I access the value for "group_id" i.e. 8

0

1 Answer 1

2

Possibly the content in response is a string and not a JSON object.

I suggest to parse the object using JSON, like so:

const o = JSON.parse(response);
console.log(o[0]);
console.log(o[0].group_id);

What you suggested (response[0].group_id) definitely should return 8 in case response is a JSON object. If you try this code below:

const o = JSON.parse("[{\"group_id\":8}]");
console.log(o[0]);
console.log(o[0].group_id);

You will get this output:

{ group_id: 8 }
8
Sign up to request clarification or add additional context in comments.

2 Comments

To be sure that the problem is the one suggested by @gil.fernandes, use of : console.log(typeof response). If it returns "string" it means that response is not an json object but a regular string.
This one worked: console.log(JSON.parse(response)[0].group_id) to give the output - 8

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.