Lines in the source and lines in the value are different

Inside a JSON source string, a backslash followed by n encodes a newline. With two backslashes, the parsed value retains a backslash and n as two characters. Both examples below are valid JSON, but the parsed text values have lengths of 3 and 4.

{"text":"A\nB"}
{"text":"A\\nB"}

JSON.parse('{"text":"A\\nB"}').text.length // 3
JSON.parse('{"text":"A\\\\nB"}').text.length // 4
Diagram comparing one LF character with a backslash and n between A and B
Original diagram based on executed fixed inputs. The numbers are the JavaScript length of the parsed text value, not the JSON file size in bytes.

A real line break inside a string is an error

RFC 8259 requires control characters U+0000 through U+001F to be escaped in strings. A literal LF inside a string falls in that range. LF, CR, tabs and spaces are allowed between tokens. In these tests, a literal LF inside the string was rejected, while a multiline object passed.

const invalid = '{"text":"A' + '\n' + 'B"}';
formatJSON(invalid); // throws

formatJSON('{\n"text":"AB"\n}'); // accepted

RFC 8259 §7: Strings · RFC 8259 §2: JSON Grammar

Separate source formatting from value interpretation

Moyoutil preserves source string tokens while changing the surrounding indentation. It does not display an escaped \n as a real line break inside the value. Minifying also preserves string escapes. If validation fails, inspect quotes and backslashes around the reported position. Adding backslashes across the entire input can change values that were already correct.

Run Format and validate with the examples, then compare the minified output. When a value needs a newline, serialize the string with JSON.stringify when generating the JSON, then check that JSON.parse returns the intended value. Passing syntax validation does not validate required fields in a particular API schema.

Frequently Asked Questions

Does minifying JSON remove newlines from values?

No. Minifying reduces whitespace outside strings. This tool preserves escapes inside strings, so newlines in the parsed values remain.