1

In Node v0.4.12,

var http = require('http');

var options = {
  host: 'example.com',
  port: 80,
  path: 'example.aspx'
};

var req = http.request(options, function(res) {
  var result = '';
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    result += chunk;
  });

  res.on('end', function () {
    var jsonStr = JSON.stringify(result);
    var data = JSON.parse(jsonStr);

    console.log(data['Date']);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

req.end();

When I tried console.log(data) it works but console.log(data['Date']) returns undefined.

Example JSON scheme:

{"Date":"17.03.2012 15:28:47", "Categories":[{"ID":1,"Name":"Foo","URLSlug":"foo"}]}

How can I fix this problem?

Thanks!

2
  • 3
    Why are you stringifying the result if it's already a string? Commented Mar 17, 2012 at 14:02
  • Thanks! I have missed that line. Commented Mar 17, 2012 at 14:12

1 Answer 1

1

If I remove all of the http request code and just attempt to parse and print out the Date part of the parsed json:

$ cat ex2.js
var data = JSON.parse('{"Date":"17.03.2012 15:28:47", "Categories":[{"ID":1,"Name":"Foo","URLSlug":"foo"}]}');
console.dir(data);
console.log(data['Date']);

$ node ex2.js
{ Date: '17.03.2012 15:28:47',
  Categories: [ { ID: 1, Name: 'Foo', URLSlug: 'foo' } ] }
17.03.2012 15:28:47

$

The example code works fine. Are you running the literal code you posted? If you are, then the error is probably that example.com isn't returning the JSON you've given as an example for example.asp. For me that returns:

$ curl -I http://example.com/example.asp
HTTP/1.0 302 Found
Location: http://www.iana.org/domains/example/
Server: BigIP
Connection: Keep-Alive
Content-Length: 0

Which is not JSON, just a 302 Redirect response. You might want to print out the full result so you can see if it's actually JSON or something else (in this case, HTML):

$ cat example.js
var http = require('http');

var options = {
  host: 'google.com',
  port: 80,
  path: '/'
};

var req = http.request(options, function(res) {
  var result = '';
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('got data back!');
    result += chunk;
  });

  res.on('end', function () {
    console.log('request is done');
    console.dir(result);
    var jsonStr = JSON.stringify(result);
    var data = JSON.parse(jsonStr);
    console.log(data['Date']);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

req.end();

For me that gives:

$ node example.js
got data back!
request is done
'<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">\n<TITLE>301 Moved</TITLE></HEAD><BODY>\n<H1>301 Moved</H1>\nThe document has moved\n<A HREF="http://www.google.com/">here</A>.\r\n</BODY></HTML>\r\n'
undefined

HTH,

Kyle

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. The problem was this line: var jsonStr = JSON.stringify(result) I removed the JSON.stringify part and the problem is solved now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.