Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Anthony Scott
PLUS
Anthony Scott
Courses Plus Student 9,001 Points

Trying to parse treehouse's JSON to get badge names.

I am trying to build off a node.js project here on Treehouse. Currently, it gets the number of badges and JavaScript points. I want to get the individual badges names and some other info. I can't figure out how to access the badge names. I am not dead set on the node path, it was just the closest example I had. Ultimately I want to display this info on a web page. Thanks!!

//require http module for status codes
const http = require('http');

//Require https module
const https = require('https');

//Print error messages
function printError(error) {
  console.error(error.message);
}

//Function to print message to console
function printMessage(username, badgeCount, points, x) {
  const message = `${username} has ${badgeCount} total badge(s) and ${points} points in JavaScript. here is a list of badges ${badgeNames}`;

    console.log(message);
}

function get(username) {
  try {
    // Connect to the API URL (https://teamtreehouse.com/username.json)
    const request = https.get(`https://teamtreehouse.com/${username}.json`, response => {
      if (response.statusCode === 200) {
          let body = "";
          // Read the data
          response.on('data', data => {
            body += data.toString();
          });

          response.on('end', () => {
              try {
                // Parse the data
                const profile = JSON.parse(body);                            
                // Print the data
                                let arr = [];
                                for(var x in profile.badges) {
                                    arr.push(profile.badges.name[x])
                                }
                printMessage(username, profile.badges.length, profile.points.JavaScript, x);
            } catch (error) {
              printError(error); //catch error from non-existent user
            }
          });
          } else {
            const message = `There was an error getting the profile for ${username} (${http.STATUS_CODES[response.statusCode]})`;
            const statusCodeError = new Error(message);
            printError(statusCodeError);
          }
      });

    request.on('error', printError);
  } catch (error) {
    printError(error);
  }
}

module.exports.get = get;                                
Steven Parker
Steven Parker
221,070 Points

This question appears to be extended with this later question.

2 Answers

Emeka Okoro
PLUS
Emeka Okoro
Courses Plus Student 11,724 Points

try this for(var x in profile.badges) { arr.push(profile.badges[x].name); } Also, you called printMessage with x as the last argument instead of arr call printMessage(username, profile.badges.length, profile.points.JavaScript, arr);

if you omit the (var x = 0; x < profile.badges.length; x++) in a for loop, the x variable is a counter that starts from 0. just inspected it without running it.

Anthony Scott
PLUS
Anthony Scott
Courses Plus Student 9,001 Points

Thanks, Emeka. That worked in Node. Steven, I decided to also try the same thing with jquery so I posted another question.