-3
const data = [{
  "detectedLanguage": {
    "language": "hi",
    "score": 1
  },
  "translations": [{
    "text": "long steff",
    "to": "en"
  }]
}]

How do I console.log only the translation.text field?

I tried console.log(JSON.stringify(res.data.translation.text) but that gives me the following error:

TypeError: Cannot read property 'text' of undefined

Not A Bot
  • 2,474
  • 2
  • 16
  • 33
  • Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – gre_gor Nov 26 '21 at 08:58

2 Answers2

1

You created arrays and am missing the index and the "s" in translations:

console.log(JSON.stringify(data[0].translations[0].text))

I also don't know why you're referencing it as res.data

Nelson
  • 2,040
  • 17
  • 23
1

Try this,

console.log(data[0].translations[0].text)

No need to use Json.stringify, because data[0].translations[0].text is not a Json, it is a string.

Georgios Syngouroglou
  • 18,813
  • 9
  • 90
  • 92