I am currently utilizing the body-parser module to parse incoming JSON objects within a POST request. My goal is to extract and store a specific value from the JSON data into a variable for later database insertion.
Below is a fragment of the code:
var http = require('http');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// Necessary for parsing the HTTP body.
// req.body holds the Object while req.rawBody contains the JSON string.
app.use(bodyParser.json()); // for parsing application/json
app.post('/', function(req, res){
var tropo = new TropoWebAPI();
parameters = req.body['session']['parameters'];
callerID = req.body['session']['from']['id'];
console.log(callerID);
if(callerID == 1234567)
{
//Action intentionally omitted
}
Unfortunately, it throws a TypeError: Cannot read property 'id' of undefined
@malix This is the structure of the JSON object:
"session": {
"id": "89c3b5d830dd8bb8b372f802aadbdfc9",
"accountId": "1234567",
"applicationId": "1234567",
"timestamp": "2016-06-23T17:09:48.685Z",
"userType": "HUMAN",
"initialText": null,
"callId": "7ab0b9306af2139a1a2e6cc8b7bd7af9",
"to": {
"id": "408XXXYYYY",
"name": "408XXXYYYY",
"channel": "VOICE",
"network": "SIP"
},
"from": {
"id": "408ZZZAAAA",
"name": "408ZZZAAAA",
"channel": "VOICE",
"network": "SIP"
},
}
I am aiming to retrieve the value 408ZZZAAAA
Your assistance is highly appreciated.