How do you log content of a JSON object in Node.js? How do you log content of a JSON object in Node.js? json json

How do you log content of a JSON object in Node.js?


Try this one:

console.log("Session: %j", session);

If the object could be converted into JSON, that will work.


function prettyJSON(obj) {    console.log(JSON.stringify(obj, null, 2));}// obj -> value to convert to a JSON string// null -> (do nothing)// 2 -> 2 spaces per indent level

JSON.stringify on MDN


To have an output more similar to the raw console.log(obj) I usually do use console.log('Status: ' + util.inspect(obj)) (JSON is slightly different).