Member preview

The workflow of accessing the attributes of a simply-created JSON object

Somebody on Hacker News just posted their jsonbin.io, perfect timing for me as I am practicing accessing JSON objects in APIs.

I created a simple JSON object — mine being TCP personified here.

{
"likes": [
"Reliability",
"Order",
"Error checks"
],
"age": 34,
"name": "TCP"
}

Then, I reused some code from a past holbertonschool.com assignment, first trying to access the whole body of the JSON object.

#!/usr/bin/node
const request = require('request');
const url = 'https://jsonbin.io/591a64459208345676e6a1ed';
request (url, function (error, response, body) {
if (error) {
console.log(error);
} else {
console.log(JSON.parse(body));
}
});

Thinking this would work, I instead got an error.

undefined:1
<!DOCTYPE html>
^
SyntaxError: Unexpected token < in JSON at position 0
at Object.parse (native)
at Request._callback (/home/vagrant/APIpractice/tcp.js:10:20)
at Request.self.callback (/usr/lib/node_modules/request/request.js:188:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/usr/lib/node_modules/request/request.js:1171:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/usr/lib/node_modules/request/request.js:1091:12)
at IncomingMessage.g (events.js:291:16)

So, I hit up stackoverflow for help, getting downvoted in the process so I have taken the question down… :(

…but I did get my answer (within 2 minutes!)…

I was accessing the wrong URL. JSON.parse() broke cause I received rendered HTML.

Here is the correct code with the correct URL.

#!/usr/bin/node
const request = require('request');
const url = 'http://jsonbin.io/b/591a64459208345676e6a1ed';
request (url, function (error, response, body) {
if (error) {
console.log(error);
} else {
console.log(JSON.parse(body));
}
});

To access the respective attributes, say the “name”, you simply add that attribute to the code accordingly.

#!/usr/bin/node
const request = require('request');
const url = 'http://jsonbin.io/b/591a64459208345676e6a1ed';
request (url, function (error, response, body) {
if (error) {
console.log(error);
} else {
console.log(JSON.parse(body).name);
}
});
>>> TCP

To access the respective an element in the array of “likes” attribute, say the first element [which is of course the 0th element], you simply add that attribute and the element you are trying to grab, to the code accordingly. [can print out all elements of the “likes” array by simply not specifying the element].

#!/usr/bin/node
const request = require('request');
const url = 'http://jsonbin.io/b/591a64459208345676e6a1ed';
request (url, function (error, response, body) {
if (error) {
console.log(error);
} else {
console.log(JSON.parse(body).likes[0]);
}
});
>>> Reliability

Lessons learned:

  • Stroll to Hacker News every so often for new products you can use to learn stuff. Even better, follow them on Twitter (@newsycombinator) or subscribe to their email newsletter (https://www.hndigest.com/)
  • When you run into errors, Stack Overflow can be helpful, but beware of downvotes. Also, earn as many reputation points as you can, so you can ask “risky” questions.
  • Use jsonbin.io to create JSON objects, access them with a JS script.
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.

Responses
Only members of Medium may see responses to this story.