-2

I am trying to access the return result of json from a request. However, no matter how i try to access one of the property I keep getting undefined

{
    "answers": [
        {
            "questions": [
                "Hello",
                "Hi",
                "Hey"
            ],
            "answer": "Hello. How may I help you today? \n\n",
            "score": 100,
            "id": 2,
            "source": "Editorial",
            "metadata": []
        },
        {
            "questions": [
                "hi",
                "hello",
                "hey"
            ],
            "answer": "Hi there. How may I assist you today?",
            "score": 100,
            "id": 5,
            "source": "Editorial",
            "metadata": []
        }
    ]
}

How do I access the answer "hello. How may I help you today" ?>

Karlo Kokkak
  • 3,674
  • 4
  • 18
  • 33
xxfirexx
  • 55
  • 1
  • 4
  • This related post should help guide you: [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json). Also, possibly: [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Jonathan Lonowski May 13 '18 at 05:51
  • We can't help you without seeing your code. Not the data, but the code. The problem is probably in how you are trying to return the response from an asynchronous call. It is probably related to [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). – jfriend00 May 13 '18 at 06:03
  • You have to add more details about how you're trying to access those properties. – calbertts May 13 '18 at 10:05

3 Answers3

0

Ensure that your JSON is stored as an object.

let obj = JSON.parse(responseJson)

Now, if you have the JSON stored in a variable obj as an object.

obj.answers[0].answer

will point to the required string.

shaahiin
  • 1,243
  • 1
  • 16
  • 26
0

You are trying to access an array of objects, so first you should access the array index and then the answer property, take a look to the example below (suppose the variable json contains the json response):

var json = {
  "answers": [
    {
      "questions": [
        "Hello",
        "Hi",
        "Hey"
      ],
      "answer": "Hello. How may I help you today? \n\n",
      "score": 100,
      "id": 2,
      "source": "Editorial",
      "metadata": [
        
      ]
    },
    {
      "questions": [
        "hi",
        "hello",
        "hey"
      ],
      "answer": "Hi there. How may I assist you today?",
      "score": 100,
      "id": 5,
      "source": "Editorial",
      "metadata": [
        
      ]
    }
  ]
}

console.log(json.answers[0].answer);
ricardoorellana
  • 2,270
  • 21
  • 35
0

Check your object is not stringified and then call its attribute

let x = {
  answers: [
    {
      questions: ["Hello", "Hi", "Hey"],
      answer: "Hello. How may I help you today? \n\n",
      score: 100,
      id: 2,
      source: "Editorial",
      metadata: []
    },
    {
      questions: ["hi", "hello", "hey"],
      answer: "Hi there. How may I assist you today?",
      score: 100,
      id: 5,
      source: "Editorial",
      metadata: []
    }
  ]
};
let output = null;
try {
  output = JSON.parse(x);
} catch (e) {
  output = x;
}

// access first answer
console.log(output.answers[0].answer);

// access all answers exist
output.answers.forEach(function(itm) {
  console.log(itm.answer);
})
Saeed
  • 5,413
  • 3
  • 26
  • 40