Skip to content

stringifyJson

stringifyJson(value, options?): string

Defined in: stringify-json.ts:95

JSON.stringify pretty-printed and ending in exactly one newline — the shape every package.json-style file on disk should have.

Pass the file’s current text as like to keep its own indent (tabs, 4 spaces, …) and line endings (\r\n) when rewriting it.

unknown

StringifyJsonOptions = {}

string

when value has no JSON representation (undefined, a function, a symbol) — JSON.stringify would return undefined for it — or when indent isn’t a non-negative integer or a string of spaces and tabs (anything else would produce text no JSON parser accepts).

import { stringifyJson } from "@theholocron/json-utils";
stringifyJson({ a: 1 }); // '{\n "a": 1\n}\n'
// rewrite a file without reformatting it
const original = await readFile("package.json", "utf8");
const pkg = JSON.parse(original);
pkg.version = "2.0.0";
await writeFile("package.json", stringifyJson(pkg, { like: original }));