A small fork of Go's encoding/json (Go 1.25) with minimal tweaks to accept JavaScript-style object keys during decoding:
- Unquoted identifier keys are allowed:
{a:1, b:2} - Single-quoted keys are allowed:
{'a':1, 'b':2} - Trailing commas are allowed:
{a:1, b:2,}and[1, 2, 3,]
Everything else behaves like encoding/json, with these differences:
- String values may be single-quoted or double-quoted. Numbers/booleans/null unchanged.
- The JavaScript literal
undefinedis accepted on decode and treated like null. - Unknown identifier values (e.g.,
myVar,someValue,window.location) are accepted on decode and treated like null. - Numbers
0and1can be unmarshaled intoboolfields (asfalseandtruerespectively). - Struct field tags use
js:"..."instead ofjson:"...". - Marshal/Encoder output remains standard JSON.
go get github.com/nukilabs/js
package main
import (
"fmt"
"github.com/nukilabs/js"
)
func main() {
var m map[string]any
_ = js.Unmarshal([]byte("{a:1, 'b':2, \"c\":3}"), &m)
fmt.Println(m) // map[a:1 b:2 c:3]
// Unknown identifiers and member expressions are treated as null
_ = js.Unmarshal([]byte("{x: someUnknownValue, y: window.location, z: 42}"), &m)
fmt.Println(m) // map[x:<nil> y:<nil> z:42]
}-
Token streaming API also supports single-quoted and bare identifier object keys.
-
This project copies Go's encoding/json under its BSD license (see LICENSE) and applies minimal changes in the scanner and decoder.