0
{
  "msg_id" : "5979ee7c",
  "_text" : "Hello Brother!",
  "entities" : {
    "intent" : [ {
      "confidence" : 0.988721779612267,
      "value" : "greeting"
    } ]
  }

How can I get the data of "value" and assign it to my another variable ? Yeah it is a simple thing but I am new to JS.

  body = JSON.parse(response.body)
  intent = (body["entities"]["intent"]).value 

When I tried to do this, it gives me the "undefined" not the real value of "value" .

So how can I initialise the intent variable over here ?

Thanks!

2 Answers2

0
intent = (body["entities"]["intent"]).value 

is supposed to be

intent = (body["entities"]["intent"])[0].value 

As intent here is an Array

Sushanth --
  • 55,259
  • 9
  • 66
  • 105
0

I hope I'm not misunderstanding your question -- you are attempting to access the value at

body.entities.intent[0].value

I believe that is how you would access the value that you want. JSON.parse returns a javascript object that you can access like an array (I think that's what you were going for) or using dot notation like I did above. There is one tricky part here which is that intent is an array that holds a single object, which is why I needed to use

intent[0] 

before I tried to access value. So the full code in your style would look like:

body = JSON.parse(response.body)
intent = body["entities"]["intent"][0].value //no parens needed here

I hope this answers your question!

minorcaseDev
  • 181
  • 5