npm.io
0.3.0 • Published 1 year ago

jsonc-reader

Licence
ISC
Version
0.3.0
Deps
0
Size
5 kB
Vulns
0
Weekly
0

JSONC reader for TypeScript

test status Code Coverage 0 dependencies install size

This micro-library (no dependencies) allows reading JSON that contain comments (JSONC). It does that by overwriting comments with whitespace - so that line and column numbers remain the same - and then loading the data as normal JSON.

import * as jsonc from "jsonc-reader"

// load JSONC from a file
const config = await jsonc.load("../my-config.jsonc")

// parse a JSONC string
const jsoncText = `
{
  // a comment
  "one": 1  // another comment
}`
const config = jsonc.parse(jsoncText)
// config === { one: 1 }

// strip comments from a JSONC string
const jsonText = jsonc.strip(jsoncText)
// jsonText === `
// {
//
//   "one": 1
// }`