1

I have a JSON object containing another JSON object like below, the size of which is not fixed. I need to access the values in the inner object.

myObj = {
"name" : "XYZ",
"sex" : "F",
"questions" : {"1":"testquestion1", "2":"testquestion2", "3":"testquestion3"}
}

I need a way to extract the [testquestion1,testquestion2,testquestion3] using a loop.

Thanks in advance!

2
  • 2
    Please read the usage description of the json tag, especially what is in all-capitals. It is confusing to use the term JSON for something that is actually JavaScript code. "JSON" is used for the text data exchange format. Commented Jul 22, 2020 at 16:07
  • stackoverflow.com/questions/2904131/… Commented Jul 22, 2020 at 16:10

1 Answer 1

5

You don't need a loop, you can use Object.values():

let myObj = {
"name" : "XYZ",
"sex" : "F",
"questions" : {"1":"testquestion1", "2":"testquestion2", "3":"testquestion3"}
}

let result = Object.values(myObj.questions);

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.