This content originally appeared on DEV Community and was authored by Gbubemi Attah
jsonvx is an elegant Go package that empowers developers with a fully configurable JSON parser, querier, and formatter—especially suitable for parsing non-standard JSON formats such as JSON5. Authored by Attah Gbubemi David (GitHub user bube054), this library is tailored for scenarios where strict compliance isn’t guaranteed and flexibility is key.
Why jsonvx Stands Out
Supports Relaxed JSON Syntax
Beyond strict JSON per ECMA-404, jsonvx allows relaxed syntax like hexadecimal numbers, single-quoted strings, unquoted keys, trailing commas, comments, and more—making it highly suitable for parsing user-generated or legacy configuration data.
Fully Configurable via ParserConfig
Control exactly which extensions are allowed—be itAllowHexNumbers
,AllowTrailingCommaArray
,AllowLineComments
,AllowSingleQuotes
,AllowJSON5
, and more. This lets you fine-tune strictness per input or use case.
Structured AST & Querying Tools
After parsing, jsonvx provides an abstract syntax tree (AST) composed of node types like Object, Array, Number, String, Boolean, and Null. Each node type offers helper methods (e.g.,AsObject
,AsNumber
) and value accessors like.Value()
to extract Go-native values.
Convenient Path-Based Querying
TheQueryPath
method allows you to dive into deeply nested data easily. For example, you can fetch fields like"name" → "first"
or even array elements like"friends" → "0" → "last"
. Keys or array indices with special characters (e.g.,"fav.movie"
) are fully supported.
Bitmap of Parsing Use Cases
Since jsonvx focuses on flexibility and correctness rather than maximum performance, it shines in use cases like building linters, CLI tools, formatters, or config loaders where input irregularities are expected.
Quick Start Example
package main
import (
"fmt"
"github.com/bube054/jsonvx"
)
func main() {
data := []byte(`{
name: { first: 'Tom', last: 'Anderson' },
age: +37,
children: ['Sara', 'Alex', 'Jack', ],
// comment
"fav.movie": 'Deer Hunter'
}`)
parser := jsonvx.NewParser(data, jsonvx.NewParserConfig(
jsonvx.WithAllowUnquoted(true),
jsonvx.WithAllowSingleQuotes(true),
jsonvx.WithAllowLeadingPlus(true),
jsonvx.WithAllowTrailingCommaArray(true),
jsonvx.WithAllowLineComments(true),
))
root, err := parser.Parse()
if err != nil {
panic(err)
}
obj, ok := jsonvx.AsObject(root)
if !ok {
panic("expected object")
}
ageNode, _ := obj.QueryPath("age")
ageNum, _ := jsonvx.AsNumber(ageNode)
ageValue, _ := ageNum.Value()
fmt.Println("Age:", ageValue) // Output: Age: 37
}
In just a few lines, this example handles comments, unquoted keys, single-quoted strings, trailing commas, and +37 as input—all thanks to jsonvx’s configurability.
Feature Highlights at a Glance
Feature | Description |
---|---|
Strict JSON + Relaxed Modes | Supports both strict JSON and optional relaxed syntax. |
Flexible ParserConfig | Toggle syntax features individually via configuration. |
AST-Based Access | Query, traverse, and manipulate JSON via structured nodes. |
Path-Based Querying | Fetch nested data via QueryPath , even in awkward keys. |
Ideal Use Cases | Tools, formatters, config parsers, linters, CLI utilities. |
Getting Started
- Installation Ensure Go is set up, then install with:
go get -u github.com/bube054/jsonvx
Import & Parse
UseNewParser(data, config)
to create a parser, then call.Parse()
.
Traverse Nodes
UseAsObject
,AsArray
,AsString
,AsNumber
, etc., and extract values through.Value()
orForEach
.
Query Paths
UseQueryPath("friends", "0", "last")
to navigate deeply nested structures.
Custom Config
Customize allowed syntactic variants usingWithAllow...
options orjsonvx.JSON5Config()
for a full JSON5 setup.
Customization Options
Each option controls whether a relaxed JSON feature is allowed. Mix and match depending on your input source.
Number Handling
-
WithAllowHexNumbers(true)
— Accept hexadecimal numbers like0x1A
. -
WithAllowLeadingPlus(true)
— Allow a leading+
in numbers (e.g.,+37
). -
WithAllowLeadingDecimalPoint(true)
— Permit.5
instead of0.5
. -
WithAllowTrailingDecimalPoint(true)
— Accept5.
instead of5.0
. -
WithAllowInfinity(true)
— ParseInfinity
and-Infinity
. -
WithAllowNaN(true)
— AcceptNaN
as a valid number.
Strings & Keys
-
WithAllowSingleQuotes(true)
— Strings can use'single quotes'
. -
WithAllowUnquoted(true)
— Object keys can be unquoted ({ name: "Tom" }
). -
WithAllowTrailingCommaArray(true)
— Arrays can end with a trailing comma. -
WithAllowTrailingCommaObject(true)
— Objects can end with a trailing comma.
Comments
-
WithAllowLineComments(true)
— Allow//
single-line comments. -
WithAllowBlockComments(true)
— Allow/* block comments */
.
JSON5 Preset
-
WithAllowJSON5(true)
Enable all JSON5-like features at once (unquoted keys, trailing commas, single quotes, comments,Infinity
,NaN
, etc.).
Closing Thoughts
jsonvx is a powerful, thoughtfully designed Go library for working with both standard and non-standard JSON. Its flexible configuration and AST-driven design make it especially ideal for scenarios where inputs are unpredictable or loose in format. Whether you’re building tools, formatters, or config loaders, jsonvx provides the right balance between strictness and flexibility.
If you find this project useful, don’t forget to star it on GitHub to show your support!
This content originally appeared on DEV Community and was authored by Gbubemi Attah