Programmatically edit JSONC in JavaScript.
This is especially useful for making programmatic changes to JSON config files. It's not recommended for very large files as this is using jsonc-parser via Wasm under the hood.
Deno:
deno add jsr:@david/jsonc-morph
Or with npm:
npm install jsonc-morph
import { parse } from "@david/jsonc-morph";
const root = parse(`{
// 1
"data" /* 2 */: 123 // 3
} // 4`);
// get the root object
const rootObj = root.asObjectOrThrow();
// set its "data" property to have a new value
rootObj.getOrThrow("data").setValue({
"nested": true,
});
// append a new key
rootObj.append("new_key", [456, 789, false]);
// inspect the output
assertEquals(
root.toString(),
`{
// 1
"data" /* 2 */: {
"nested": true
}, // 3
"new_key": [456, 789, false]
} // 4`,
);