Skip to content

dsherret/jsonc-morph

Repository files navigation

jsr:@david/jsonc-morph / npm:jsonc-morph

JSR npm version

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.

Install

Deno:

deno add jsr:@david/jsonc-morph

Or with npm:

npm install jsonc-morph

Example

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`,
);

About

Programmatically edit JSONC in JavaScript.

Resources

License

Stars

Watchers

Forks

Packages

No packages published