0
var request = require('request');

request.post(
    'http://www.yoursite.com/formpage',
    { json: { key: 'value' } },
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body)
        }
    }
);

But the issue I have is not knowing how to parse and adjust the code.

"entities": [
    {
      "entity": "today",
      "type": "builtin.datetimeV2.date",
      "startIndex": 0,
      "endIndex": 4,
      "resolution": {
        "values": [
          {
            "timex": "2017-12-13",
            "type": "date",
            "value": "2017-12-13"
          }
        ]
      }
    }
  ]
}

The value I want from this JSON is today in entity.

How can I adjust the code to give me that exact entity I want ?

krlzlx
  • 5,752
  • 14
  • 47
  • 55
Alexis
  • 95
  • 1
  • 7

2 Answers2

0

entities is an Array, so if you want to get the first element of entities, you just have to do something like this:

console.log(body.entities[0].entity);

Hope it helps.

Sparw
  • 2,688
  • 1
  • 15
  • 34
  • The thing is i don't want such a pin point method because i'll be checking the type, and if the type matchs the one i'm expecting i'll take that enitty value. – Alexis Dec 13 '17 at 15:22
0

you can use for example Chrome Developer tools, if you are unsure. Copy & paste the complete JSON string, and then press

Ctrtl + Shift + J

testvar = {"entities": [
{
  "entity": "today",
  "type": "builtin.datetimeV2.date",
  "startIndex": 0,
  "endIndex": 4,
  "resolution": {
    "values": [
          {
            "timex": "2017-12-13",
             "type": "date",
             "value": "2017-12-13"
           }
         ]
       }
     }
   ]
 };

Now you can just enter

testvar

into the console window

And you will see:

{entities: Array(1)}

you can now expand the variable by clicking the expand icon.

Entering

testvar.entities[0] 

will show you everthing below the first array item. Entering

testvar.entities[0].entity

will show

today

This approach using developer tools can be easier if you are new to JSON or the structures are too nested.

In addition to your comment

"The thing is i don't want such a pin point method because i'll be checking the type, and if the type matchs the one i'm expecting i'll take that enitty value"

You can always check whether a node exists.

For example....

   typeof(body);
   "object"

   typeof(body.entities);
   "object"

   typeof(body.testxyz);
   "undefined"

   typeof(body.entities[0].resolution.values);
   "object"

You can check for these in your conditions.

Additionally, you can retrieve the number of items within a node.

   Object.keys(body.entities[0].resolution.values).length

Hope this helps.

chingo81
  • 61
  • 5