DEV Community

Cover image for Introducing jsonvx: The Most Customisable JSON Parser, Querier & Formatter for Go
Gbubemi Attah
Gbubemi Attah

Posted on

Introducing jsonvx: The Most Customisable JSON Parser, Querier & Formatter for Go

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 it AllowHexNumbers, 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

    The QueryPath 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
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Import & Parse

    Use NewParser(data, config) to create a parser, then call .Parse().

  • Traverse Nodes

    Use AsObject, AsArray, AsString, AsNumber, etc., and extract values through .Value() or ForEach.

  • Query Paths

    Use QueryPath("friends", "0", "last") to navigate deeply nested structures.

  • Custom Config

    Customize allowed syntactic variants using WithAllow... options or jsonvx.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 like 0x1A.
  • WithAllowLeadingPlus(true) — Allow a leading + in numbers (e.g., +37).
  • WithAllowLeadingDecimalPoint(true) — Permit .5 instead of 0.5.
  • WithAllowTrailingDecimalPoint(true) — Accept 5. instead of 5.0.
  • WithAllowInfinity(true) — Parse Infinity and -Infinity.
  • WithAllowNaN(true) — Accept NaN 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!

Top comments (2)

Collapse
 
onlineproxy profile image
OnlineProxy

I’ve dealt with JSON5 before, mostly for config files, and yeah… it usually means dragging in some third-party library or duct-taping a custom solution together. So having a parser that just gets it and handles those quirks right out of the box that’s a workflow dream come true - especially when you're knee-deep in user-generated mess or dusty old legacy formats. From what I’ve seen, jsonvx looks like it might actually strike that sweet balance between being strict when you need it and chill when you don’t. That kind of flexibility super clutch when you’re dealing with wild-west JSON in the real world.

Collapse
 
gbubemi_attah_8220489db16 profile image
Gbubemi Attah • Edited

Thanks man 💯💯💯