Skip to content
Effect 4.0 is here. One ecosystem. Zero dependencies.

Jsonc unstable

Parses JSON with JavaScript-style comments without external dependencies.

The syntax target is the JSONC draft specification with jsonc-parser 3.3.1's default profile: JSON plus line and block comments, with trailing commas disabled. One leading BOM is accepted, matching the TOML and YAML parsers. Decoded values follow JSON.parse semantics; invalid documents fail instead of returning a partial result.

1 exports Since v4.1.2 Source

Decoding

parse

unstable
Since v4.1.2

Parses a JSONC document into a JavaScript value.

Details

Returns Result.succeed with the parsed value, or Result.fail with an EncodingError when the input is not valid JSONC. Accepts one leading BOM and line and block comments wherever JSON permits whitespace. All other syntax follows JSON.parse, including duplicate keys and __proto__ handling.

Gotchas

Error messages give a 1-based line and column, counted in UTF-16 code units, but do not include the input text. Trailing commas and JSON5 extensions are not accepted. Unlike JSON.parse, this function has no reviver.

Signature

declare function parse(input: string): Result<unknown, EncodingError>

Example Parsing a JSONC document

import { Result } from "effect"
import { Jsonc } from "effect/encoding"
Jsonc.parse("{ \"port\": 8080 } // comment") // => Result.succeed({ port: 8080 })
Result.isFailure(Jsonc.parse("{ \"port\": 8080, }")) // => true