Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Node.js Basics (2014) Building a Command Line Application Parsing JSON

Mark Hill
Mark Hill
9,047 Points

parse JSON

Trying to follow the introduction to node, sending request to API to get JSON in return, but I can't seem to get anything back. When I visit address with browser, a json appears, but when I use this code, I don't seem to get anything back - not really too sure where I'm going wrong. Code:

var http = require('http');
var username = "chalkers";
var body = "";

var request = http.get("http://teamtreehouse.com/" + username + ".json", function(response){
  response.on('data', function(chunk){
    body += chunk;
    console.log(body);
  });
  response.on('end', function(){
      var profile = JSON.parse(body);
      console.log(profile);
  });
});
request.on("error", function(error){
  console.log(error.message);
});

error message:

SyntaxError: Unexpected end of JSON input                                                                    
    at Object.parse (native)                                                                                 
    at IncomingMessage.<anonymous> (/home/treehouse/workspace/app.js:11:26)                                  
    at emitNone (events.js:91:20)                                                                            
    at IncomingMessage.emit (events.js:185:7)                                                                
    at endReadableNT (_stream_readable.js:934:12)                                                            
    at _combinedTickCallback (internal/process/next_tick.js:74:11)                                           
    at process._tickCallback (internal/process/next_tick.js:98:9) 

2 Answers

Thomas Nilsen
Thomas Nilsen
14,957 Points

They have changed their api. use https (notice the s), instead of http.

var https = require('https');
var username = "chalkers";
var body = "";

var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response){
  response.on('data', function(chunk){
    body += chunk;
    console.log(body);
  });
  response.on('end', function(){
      var profile = JSON.parse(body);
      console.log(profile);
  });
});
request.on("error", function(error){
  console.log(error.message);
});

Hello,

This error message is failing at the parse, not the response. You can actually replicate this and remove the response.on section and it will work fine, when you add it back it will fail to clarify.