Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We have the response body as a String. Let's convert it into an Object we can access the data more programmatically.
Documentation
We have the string printing out, but
0:00
we want to access the properties
programmatically.
0:02
We want to convert
the string into an object.
0:05
The process of converting a string into
a data structure is called parsing.
0:09
You may not know this, but if you look
at the documentation to parse JSON,
0:13
it can't be found as a host object
in the Node.js environment,
0:17
it's actually a native object.
0:21
A good reference for native objects
is the Mozilla Developer Network.
0:24
I've included a link in
the teacher's notes.
0:28
As you can see,
there's a method called parse.
0:31
You can pass in a string to
get parsed into a JSON object.
0:34
Let's add the functionality to our code.
0:39
We'll store the object as
a constant variable called Profile.
0:41
We can use console.dir,
To take a look inside the profile.
0:56
We can use the DIR output to figure out
how many number of badges we've got and
1:05
how many points in
the JavaScript topic area.
1:09
We have a badges array, so
we can print out the message.
1:17
Profile.badges.length, which
will be our badge count.
1:26
Finally, we have a points object that
is segmented into each topic area.
1:36
We want the JavaScript points.
1:43
So we can do profile.points.JavaScript.
1:46
Let's try this out and see what happens.
2:01
There I am with my badge count and
JavaScript points.
2:06
You need to sign up for Treehouse in order to download course files.
Sign up