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

Saad Khan Malik
Saad Khan Malik
25,199 Points

How would I type a sting based key?

Playing around with the code I've gotten the "profile.points.JavaScript " to work, buts what's the solution when its " profile.points.'Development tools' " as in the key is a string?

Here's my code for reference.

//Problem   : We need a simple way to look up  a user's badgecount and javascript points.
//solution  Use Node.js to connect te teamtreehouse's API to get profile information to print out

var https = require("https");
var username ="saadkhanmalik";


function printMessage(username, badgeCount, points) {
  var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in JavaScript";
  console.log(message);
}


//connect to the api url:
//https://teamtreehouse.com/saadkhanmalik.json
var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
  //console.log(response.statusCode); //Used the to innitially check connection.
  var body = "";
  //read the data
  response.on('data', function (chunk){
    body += chunk;
  });
  response.on('end', function(){
    // console.log(body);
    // console.log(typeOf body);
    var profile = JSON.parse(body);
    var totalPoints = ( profile.points.HTML +
                        profile.points.CSS +
                        profile.points.Design +
                        profile.points.JavaScript +
                        profile.points.Ruby +
                        profile.points.PHP +
                        profile.points.WordPress +
                        profile.points.iOS +
                        //profile.points.'Development Tools' +
                        profile.points.Business +
                        profile.points.Python +
                        profile.points.Java +
                        // profile.points.'Digital Literacy' +
                        // profile.points.'Game Development' +
                        // profile.points.'C#' +
                        profile.points.Databases
                      )

    console.log(totalPoints);
    printMessage(username, profile.badges.length, profile.points.JavaScript)
    //console.dir(profile); //prints entier profile

1 Answer

Jesus Mendoza
Jesus Mendoza
23,289 Points

Using the brackets syntax.

profile.points['Development Tools']