Skip to content

console: add console.json() method - #65251

Closed
magicmark wants to merge 4 commits into
nodejs:mainfrom
magicmark:console-json
Closed

console: add console.json() method#65251
magicmark wants to merge 4 commits into
nodejs:mainfrom
magicmark:console-json

Conversation

@magicmark

@magicmark magicmark commented Aug 12, 2026

Copy link
Copy Markdown

Adds console.json(value[, replacer[, space]]) as a thin wrapper over JSON.stringify. Arguments pass through directly, so the caller controls formatting — no default indentation imposed.

Equivalent to:

console.json = (...args) => console.log(JSON.stringify(...args));

The motivation is inspecting objects without util.inspect's annotations. The canonical case is null-prototype objects, which print as [Object: null prototype] { foo: 'bar' } via console.log but cleanly as {"foo":"bar"} via JSON.stringify.

Refs: whatwg/console#222


Draft PR: This PR was sent by Claude and may not have yet been reviewed by mark.

Adds `console.json(...values)` which serializes each argument using
`JSON.stringify` with 2-space indentation, printing to stdout. Each
value is printed on a separate line.

Motivation: inspecting objects via `console.log` applies `util.inspect`
formatting, which adds annotations like `[Object: null prototype]` that
are noise when you just want the plain data. `JSON.stringify` already
handles null-prototype objects cleanly, so this is a thin wrapper that
makes the common pattern `console.log(JSON.stringify(x, null, 2))`
first-class.

Non-serializable top-level values (undefined, functions, symbols) are
silently skipped, matching the behavior of JSON.stringify returning
undefined for them. Circular references throw a TypeError.

Refs: whatwg/console#222

Co-Authored-By: Claude <noreply@anthropic.com>
@nodejs-github-bot nodejs-github-bot added console Issues and PRs related to the console subsystem. needs-ci PRs that need a full CI run. labels Aug 12, 2026
magicmark and others added 3 commits August 12, 2026 15:54
Pass all arguments directly to JSON.stringify, matching the semantics of
console.json = (...args) => console.log(JSON.stringify(...args)).
No default indentation — caller controls formatting via JSON.stringify's
own replacer/space arguments.

Co-Authored-By: Claude <noreply@anthropic.com>
Serialize each argument independently rather than splatting all args
into JSON.stringify — so console.json(a, b) prints two lines, not
treating b as a replacer.

Co-Authored-By: Claude <noreply@anthropic.com>
Match JSON.stringify's own signature rather than using rest args.

Co-Authored-By: Claude <noreply@anthropic.com>

@avivkeller avivkeller left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node.js cannot land a console method not in the Console specification.

@avivkeller avivkeller closed this Aug 12, 2026
@avivkeller

avivkeller commented Aug 12, 2026

Copy link
Copy Markdown
Member

I'm closing this PR as it does not abide by our AI Guidelines. Specifically, the "Own every line you submit" clause has been violated, as the commits have not been signed off by the author, and your agent wrote that it's contents were not human reviewed in the description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

console Issues and PRs related to the console subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants