-1

I've created a small Node app to get the jobs from the GitHub jobs API. I'm using the module request to do this. You can see the code below:

const request = require("request");
const url ="https://jobs.github.com/positions.json?search=remote";
request.get(url, (error, response, body) => {
  let json = JSON.parse(body);
  console.log(
     `Data: ${json}`,
  );
});

I would appreciate your help printing a JSON object, at the moment it just prints [object, Object].

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

2 Answers2

3
console.log(JSON.stringify(json, null, 2));

will pretty print it with two spaces indentation. You can leave out the second and third argument to print the json without pretty printing it.

Also see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Ingo Bürk
  • 19,263
  • 6
  • 66
  • 100
2

Just use the JavaScript method JSON.stringify() in order to convert the JSON to string for printing in the console.

const request = require("request");
const url ="https://jobs.github.com/positions.json?search=remote";
request.get(url, (error, response, body) => {
  let json = JSON.parse(body);
  console.log(JSON.stringify(json));
});
Avivbuen
  • 128
  • 9