SIGN IN SIGN UP

jq: accept literal control chars inside JSON strings (#214)

* jq: accept literal control chars inside JSON strings

Real jq tolerates JSON strings containing unescaped control characters
(newlines, tabs, carriage returns, etc.) even though RFC 8259 forbids
them. Real-world inputs — webhooks, log lines piped through jq, output
from terminals copy-pasted into a script — frequently contain literal
tabs/newlines inside string values, and rejecting those at the
JSON.parse boundary surprises users who expect parity with jq.

Pre-process the slice we hand to JSON.parse: walk the string tracking
quote/escape state, and when a control char (≤ 0x1f) appears inside a
string, replace it with its JSON escape (\n, \t, \r, \b, \f, or \uXXXX).
Outside strings the input is left untouched, so structural whitespace
keeps its meaning.

Tests cover newline, tab, and CR inside strings; preserved escape
sequences alongside literal control chars; control chars outside
strings (whitespace) leaving compact output unchanged; and concatenated
JSON values where one contains a literal newline.

* add changeset
Z
Zach Smith committed
da58f4f523c5e9c1c444106a0f2a7777a59fb618
Parent: a35a75f
Committed by GitHub <noreply@github.com> on 5/5/2026, 7:25:27 PM