Skip to content

GJSON

A fast Go library for pulling values out of JSON using simple dot-style paths.

Stars
15.6k
Forks
2.2k
Licence
MIT
Language
Go
Last update
1 months ago
Type
Library

What it is

GJSON is a Go package that reads values from a JSON document using short path strings such as "name.last" or "age". You give it the JSON text and a path, and it returns the matching value without you having to define structs or fully parse the document. It also supports array queries, iteration, and modifier functions for simple transformations.

  • Read a nested field such as name.last from an API response in one call
  • Count array elements with a path like children.#
  • Find the first or all array items matching a condition, such as friends.#(age>45)#.last
  • Reverse or reformat parts of a JSON document using built-in modifiers like @reverse or @pretty

Great for

  • Go developers who need a few fields from a JSON response without writing struct definitions
  • Services that read large JSON payloads and want to avoid full unmarshalling for simple lookups
  • Developers who want to filter array elements with short query expressions
  • Projects that process JSON Lines input

Not ideal for

  • Projects that need to modify JSON; the README points to the separate SJSON project for that
  • Non-Go projects; the README mentions Python and Rust ports maintained separately, not this Go package
  • Anyone needing full schema validation or typed mapping of whole documents, since this focuses on path-based lookups
Step-by-step guide: how to use GJSONInstall, first run, examples, tips and FAQs

How to get it

  1. 1

    Install with a package manager

    You'll need: Go

    Install the library

    go get -u github.com/tidwall/gjson
  2. 2

    Run it

    • Install Go and run the go get command shown in the README
    • Import github.com/tidwall/gjson in a Go file
    • Call gjson.Get(json, "name.last") with a JSON string and a path
    • Call .String() on the result to print the value
  3. 3

    Get the most out of it

    • Use GetBytes when your JSON is already a byte slice
    • Use the #(...)# query syntax to collect all matching array items
    • Chain paths with the pipe character, for example children|@reverse|0
    • Pass options to @pretty, such as sortKeys, to format output
    • Register custom modifiers with gjson.AddModifier for project-specific transforms

Downloads

Strengths

  • Simple one-line lookups using dot-notation paths, as shown in the README examples
  • Supports 64-bit integer reads through Int() and Uint()
  • Extensible with custom modifiers via AddModifier

Trade-offs

  • The README's performance claims are linked but not verified here; treat speed as unconfirmed by this review
  • Query syntax and modifiers have their own learning curve, and the full details live in SYNTAX.md
  • Older bracket query syntax #[...] is deprecated and only kept until the next major release

How it compares

ProjectStarsTypeLevelUpdatedBest for
gjson (this)15.6kLibrarybeginner1 months agoGo developers who need a few fields from a JSON response without writing struct definitions
go13.9kLibrarybeginner2.4 years agoGo developers who parse or produce large amounts of JSON and want to test a faster option
json50.8kLibrarybeginner4 days agoC++ developers who need to read or write JSON in an application
SwiftyJSON22.9kLibrarybeginner3 months agoSwift developers building iOS, macOS, tvOS or watchOS apps that read JSON from web APIs
zerolog12.5kLibrarybeginner23 days agoGo developers who want machine-readable JSON logs
yq16.2kCLIbeginner8 days agoDevelopers who regularly edit YAML config files in scripts or CI pipelines

Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.