title: Accessing field on Json Object in Node.js
body: <p>So I'm new to JavaScript programming. I'm getting this JSON payload on a POST request:</p>
  
  <pre><code>{
    "subscriptionId" : "asdasdasdasd",
    "originator" : "localhost",
    "contextResponses" : [
      {
        "contextElement" : {
          "type" : "",
          "isPattern" : "false",
          "id" : "id",
          "attributes" : [
            {
              "name" : "temperature",
              "type" : "int",
              "value" : "5"
            }
          ]
        },
        "statusCode" : {
          "code" : "200",
          "reasonPhrase" : "OK"
        }
      }
    ]
  }
  </code></pre>
  
  <p>And using this code to try and access a certain field:</p>
  
  <pre><code>var qs = require('querystring');
  var http = require('http');
  http.createServer(function (req, res) {
  var obj;
  
  if (req.method == 'POST') {
          var body = '';
          req.on('data', function (data) {
              body += data;
              console.log(body.attributes.value);
              if (body.length &gt; 1e6)
                  req.connection.destroy();
          });
  
          req.on('end', function () {
              var post = qs.parse(body);
              // use post['blah'], etc.
          });
      }
  }).listen(8087, "188.???.??.???");
  console.log('Server running at http://188.???.??.???:8087/');
  </code></pre>
  
  <p>As you can see I'm trying to access the value field and trying to retrieve the number 5. Obviously, it's not working. I tried googleing it and this is probably something really silly. Any suggestions on how to access the field?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The data from console.log(body):</p>
  
  <pre><code>Server running at http://????????
  {
    "subscriptionId" : "asdasdasdasd",
    "originator" : "localhost",
    "contextResponses" : [
      {
        "contextElement" : {
          "type" : "",
          "isPattern" : "false",
          "id" : "fiwaresensorfinal",
          "attributes" : [
            {
              "name" : "temperature",
              "type" : "int",
              "value" : "5"
            }
          ]
        },
        "statusCode" : {
          "code" : "200",
          "reasonPhrase" : "OK"
        }
      }
    ]
  }
  </code></pre>
  
  <p>The console.log(data):</p>
  
  <pre><code>&lt;Buffer 7b 0a 20 20 22 73 75 62 73 63 72 69 70 74 69 6f 6e 49 64 22 20 3a 20 22 35 35 38 31 37 62 33 62 39 38 61 64 64 31 38 63 63 33 65 31 38 33 62 65 22 2c 0a ...&gt;
  </code></pre>
  
  <p>The console.log(recv):</p>
  
  <pre><code>{ subscriptionId: '55817b3b98add18cc3e183be',
    originator: 'localhost',
    contextResponses: [ { contextElement: [Object], statusCode: [Object] } ] }
  </code></pre>