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!

Arpa Ray
  • 21
  • 4
  • 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. – trincot Jul 22 '20 at 16:07
  • https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation – Teemu Jul 22 '20 at 16:10

1 Answers1

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);
mickl
  • 48,568
  • 9
  • 60
  • 89