diff --git a/doc/api/console.md b/doc/api/console.md index a8369290dfbf..691f54ad9e57 100644 --- a/doc/api/console.md +++ b/doc/api/console.md @@ -328,6 +328,39 @@ added: v0.1.100 The `console.info()` function is an alias for [`console.log()`][]. +### console.json(value[, replacer[, space]]) + +* `value` {any} +* `replacer` {Function|Array|null} +* `space` {number|string} + +Prints `JSON.stringify(value, replacer, space)` to `stdout`. Arguments are +passed directly to [`JSON.stringify()`][]. This is useful for inspecting +objects without the annotations added by [`util.inspect()`][] (such as +`[Object: null prototype]`). + +```js +console.json({ foo: 'bar' }); +// Prints: {"foo":"bar"} + +console.json({ foo: 'bar' }, null, 2); +// Prints: +// { +// "foo": "bar" +// } + +// Works cleanly with null-prototype objects +const obj = Object.assign(Object.create(null), { foo: 'bar' }); +console.json(obj); +// Prints: {"foo":"bar"} +``` + +Throws `TypeError` for non-serializable values such as circular references. +Use [`console.log()`][] or [`console.dir()`][] for values that may not be +JSON-serializable. + ### console.log([data][, ...args])