Browse by type
It does not require you to know the structure of the payload (eg. create structs), and allows accessing fields by providing the path to them. It is up to 6x faster than standard encoding/json package (depending on payload size and usage), allocates no memory. See benchmarks below.
jsonparser is the reference case study for ReqProof — a git-native requirements-engineering and formal-verification platform. Every public API is traced to a formal requirement, every requirement is tested with 100% Modified Condition/Decision Coverage (MC/DC), and the entire parser is fuzzed by a custom structure-aware JSON fuzzer (github.com/probelabs/json-fuzz) that generates grammar-valid mutations at 250,000 inputs/second.
| Metric | Value |
|---|---|
| Requirements traced | 118 (7 stakeholder + 111 system) |
| Proof audit | 0 errors, 0 warnings (L3 strict) |
| Code-level MC/DC | 100% decisions, 100% conditions |
| Requirement-side MC/DC | 377/377 witness rows covered |
| Fuzz executions | 16M+ (structure-aware + path-mutation + encoding/json differential) |
| Bugs found & fixed by the proof review | 7 (4 panics, 2 data-corruption, 1 encoding bug) |
The proof review caught bugs that years of community use, OSS-Fuzz, and standard fuzzing had missed — including a panic class across 8 unchecked-dereference sites, a silent data-loss bug in Set, and a malformed-output bug in Delete. Read the full root-cause analysis →
Originally I made this for a project that relies on a lot of 3rd party APIs that can be unpredictable and complex.
I love simplicity and prefer to avoid external dependecies. encoding/json requires you to know exactly your data structures, or if you prefer to use map[string]interface{} instead, it will be very slow and hard to manage.
I investigated what's on the market and found that most libraries are just wrappers around encoding/json, there is few options with own parsers (ffjson, easyjson), but they still requires you to create data structures.
Goal of this project is to push JSON parser to the performance limits and not sacrifice with compliance and developer user experience.
For the given JSON our goal is to extract the user's full name, number of github followers and avatar.
import "github.com/buger/jsonparser"
...
data := []byte(`{
"person": {
"name": {
"first": "Leonid",
"last": "Bugaev",
"fullName": "Leonid Bugaev"
},
"github": {
"handle": "buger",
"followers": 109
},
"avatars": [
{ "url": "https://avatars1.githubusercontent.com/u/14009?v=3&s=460", "type": "thumbnail" }
]
},
"company": {
"name": "Acme"
}
}`)
// You can specify key path by providing arguments to Get function
jsonparser.Get(data, "person", "name", "fullName")
// There is `GetInt` and `GetBoolean` helpers if you exactly know key data type
jsonparser.GetInt(data, "person", "github", "followers")
// When you try to get object, it will return you []byte slice pointer to data containing it
// In `company` it will be `{"name": "Acme"}`
jsonparser.Get(data, "company")
// If the key doesn't exist it will throw an error
var size int64
if value, err := jsonparser.GetInt(data, "company", "size"); err == nil {
size = value
}
// You can use `EachArray` helper to iterate items [item1, item2 .... itemN]
jsonparser.EachArray(data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
fmt.Println(jsonparser.Get(value, "url"))
}, "person", "avatars")
// Or use can access fields by index!
jsonparser.GetString(data, "person", "avatars", "[0]", "url")
// You can use `EachObject` helper to iterate objects { "key1":object1, "key2":object2, .... "keyN":objectN }
jsonparser.EachObject(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
fmt.Printf("Key: '%s'\n Value: '%s'\n Type: %s\n", string(key), string(value), dataType)
return nil
}, "person", "name")
// The most efficient way to extract multiple keys is `EachKey`
paths := [][]string{
[]string{"person", "name", "fullName"},
[]string{"person", "avatars", "[0]", "url"},
[]string{"company", "url"},
}
jsonparser.EachKey(data, func(idx int, value []byte, vt jsonparser.ValueType, err error){
switch idx {
case 0: // []string{"person", "name", "fullName"}
...
case 1: // []string{"person", "avatars", "[0]", "url"}
...
case 2: // []string{"company", "url"},
...
}
}, paths...)
// For more information see docs below
The package-level functions remain strict RFC 8259 parsers. For inputs that use
single-quoted strings or non-standard escapes, use a Config explicitly:
data := []byte(`{'name':'Ada','role':'engineer'}`)
name, err := jsonparser.Lenient.GetString(data, "name")
// name == "Ada"
Lenient enables both compatibility options. You can also enable only the
extension your input requires:
config := jsonparser.Config{AllowUnknownEscapes: true}
data := []byte("{\"path\":\"docs\\`draft\\x\"}")
path, err := config.GetString(data, "path")
// path == "docs`draftx"
The config-aware Get, GetString, Set, Delete, ArrayEach, and
ObjectEach methods share the same signatures and behavior as their
package-level counterparts apart from the enabled parsing extensions.
jsonparser.DefaultConfig is strict; jsonparser.Lenient enables
AllowSingleQuotes and AllowUnknownEscapes.
ReaderParser provides the same path-based lookup model for JSON read from an
io.Reader, so a large document does not need to be loaded into a single byte
slice:
file, err := os.Open("large.json")
if err != nil {
log.Fatal(err)
}
defer file.Close()
parser := jsonparser.NewReaderParser(file)
name, err := parser.GetString("person", "name")
To process a root array incrementally, use ArrayEach. Each callback value is
valid for the duration of the callback; copy it if it must be retained:
parser := jsonparser.NewReaderParser(file)
err := parser.ArrayEach(func(value []byte, valueType jsonparser.ValueType, err error) {
if err != nil {
return
}
process(value, valueType)
})
The parser reads in 64 KiB chunks and discards completed prefixes. Its default
sliding-window target is 64 MiB; customize it with
jsonparser.Config{MaxBufferSize: size}. A single returned value or array
element may exceed that target because its complete bytes are supplied to the
caller. Create a new ReaderParser for each lookup or array traversal.
ReaderParser also honors AllowSingleQuotes and AllowUnknownEscapes.
Library API is really simple. You just need the Get method to perform any operation. The rest is just helpers around it.
You also can view API at godoc.org
Getfunc Get(data []byte, keys ...string) (value []byte, dataType jsonparser.ValueType, offset int, err error)
Receives data structure, and key path to extract value from.
Returns:
* value - Pointer to original data structure containing key value, or just empty slice if nothing found or error
* dataType - Can be: NotExist, String, Number, Object, Array, Boolean or Null
* offset - Offset from provided data structure where key value ends. Used mostly internally, for example for ArrayEach helper.
* err - If the key is not found or any other parsing issue, it should return error. If key not found it also sets dataType to NotExist
Accepts multiple keys to specify path to JSON value (in case of quering nested structures).
If no keys are provided it will try to extract the closest JSON value (simple ones or object/array), useful for reading streams or arrays, see ArrayEach implementation.
Note that keys can be an array indexes: jsonparser.GetInt("person", "avatars", "[0]", "url"), pretty cool, yeah?
GetStringfunc GetString(data []byte, keys ...string) (val string, err error)
Returns strings properly handing escaped and unicode characters. Note that this will cause additional memory allocations.
GetUnsafeStringIf you need string in your app, and ready to sacrifice with support of escaped symbols in favor of speed. It returns string mapped to existing byte slice memory, without any allocations:
s, _, := jsonparser.GetUnsafeString(data, "person", "name", "title")
switch s {
case 'CEO':
...
case 'Engineer'
...
...
}
Note that unsafe here means that your string will exist until GC will free underlying byte slice, for most of cases it means that you can use this string only in current context, and should not pass it anywhere externally: through channels or any other way.
GetBoolean, GetInt and GetFloatfunc GetBoolean(data []byte, keys ...string) (val bool, err error)
func GetFloat(data []byte, keys ...string) (val float64, err error)
func GetInt(data []byte, keys ...string) (val int64, err error)
If you know the key type, you can use the helpers above. If key data type do not match, it will return error.
EachArrayfunc EachArray(data []byte, cb func(value []byte, dataType jsonparser.ValueType, offset int, err error), keys ...string)
Needed for iterating arrays, accepts a callback function with the same return arguments as Get.
ArrayEach remains available as a backward-compatible alias.
The error-returning and wildcard variants follow the same naming convention:
use EachArrayErr and EachArrayWildcard; ArrayEachErr and
ArrayEachWildcard remain available for backward compatibility.
EachObjectfunc EachObject(data []byte, callback func(key []byte, value []byte, dataType ValueType, offset int) error, keys ...string) (err error)
Needed for iterating object, accepts a callback function. Example:
var handler func([]byte, []byte, jsonparser.ValueType, int) error
handler = func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
//do stuff here
}
jsonparser.EachObject(myJson, handler)
ObjectEach remains available as a backward-compatible alias.
EachKeyfunc EachKey(data []byte, cb func(idx int, value []byte, dataType jsonparser.ValueType, err error), paths ...[]string)
When you need to read multiple keys, and you do not afraid of low-level API EachKey is your friend. It read payload only single time, and calls callback function once path is found. For example when you call multiple times Get, it has to process payload multiple times, each time you call it. Depending on payload EachKey can be multiple times faster than Get. Path can use nested keys as well!
paths := [][]string{
[]string{"uuid"},
[]string{"tz"},
[]string{"ua"},
[]string{"st"},
}
var data SmallPayload
jsonparser.EachKey(smallFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error){
switch idx {
case 0:
data.Uuid, _ = value
case 1:
v, _ := jsonparser.ParseInt(value)
data.Tz = int(v)
case 2:
data.Ua, _ = value
case 3:
v, _ := jsonparser.ParseInt(value)
data.St = int(v)
}
}, paths...)
Setfunc Set(data []byte, setValue []byte, keys ...string) (value []byte, err error)
Receives existing data structure, key path to set, and value to set at that key. This functionality is experimental.
Returns:
* value - Pointer to original data structure with updated or added key value.
* err - If any parsing issue, it should return error.
Accepts multiple keys to specify path to JSON value (in case of updating or creating nested structures).
Note that keys can be an array indexes: jsonparser.Set(data, []byte("http://github.com"), "person", "avatars", "[0]", "url")
Deletefunc Delete(data []byte, keys ...string) value []byte
Receives existing data structure, and key path to delete. This functionality is experimental.
Returns:
* value - Pointer to original data structure with key path deleted if it can be found. If there is no key path, then the whole data structure is deleted.
Accepts multiple keys to specify path to JSON value (in case of updating or creating nested structures).
Note that keys can be an array indexes: jsonparser.Delete(data, "person", "avatars", "[0]", "url")
Appendfunc Append(data []byte, value []byte, keys ...string) ([]byte, error)
Appends value to the end of the JSON array addressed by keys. When keys is
empty, Append addresses the top-level value. If a keyed path does not exist,
Append creates it as a single-element array using Set's auto-vivification
behavior. Returns MalformedArrayError if the addressed value is not an array.
// Append to an array without knowing its length
data, _ = jsonparser.Append(data, []byte(`"new_item"`), "items")
encoding/json, reflection or interface{}, the only real package dependency is bytes.There are 3 benchmark types, trying to simulate re
—
browse all types & interfaces →
$ claude mcp add jsonparser \
-- python -m otcore.mcp_server <graph>