gitmyhub

gjson

Go ★ 16k updated 1mo ago

Get JSON values quickly - JSON parser for Go

GJSON is a tiny Go library that extracts values from JSON documents using a simple dot-notation path, no structs needed, just pass the JSON and a path like 'name.last' and get the value back instantly.

Gosetup: easycomplexity 2/5

GJSON is a small library for the Go programming language that pulls specific values out of a JSON document quickly and with very little code. JSON is the format most web APIs use to send data — a nested set of names, values, and lists — and normally you have to define matching data structures in Go just to read one field. GJSON skips that step: you hand it a JSON string and a path that describes where in the document the value lives, and it returns the value directly.

Paths use a simple dot syntax that looks a lot like spreadsheet cell references: "name.last" reaches into an object, "children.1" gets the second item in an array, and "children.#" gives the number of items. The "#" character also opens up array queries with comparison operators (equals, greater-than, less-than, "like" pattern matching) so you can ask for things like the last name of every friend over 45. Wildcards (* and ?) let you match keys you only partly know. Results come back as a Result type that can be turned into the obvious Go types — a string, an integer, a float, a boolean, or even a time. Modifiers, written with an @ prefix, can transform the result mid-path — @reverse flips an array, @pretty and @ugly reformat the JSON, @keys and @values pull apart objects — and a pipe character chains them.

You would use GJSON whenever you need to read a JSON response or file in Go without writing a struct for it: quick scripts, command-line tools, log processors, or for digging into unfamiliar payloads. A companion library called SJSON handles modifying JSON, and a command-line tool called JJ wraps the same engine. Ports also exist for Python and Rust. The full README is longer than what was provided.

Where it fits