{"id":13413251,"url":"https://github.com/valyala/fastjson","last_synced_at":"2025-05-13T17:09:41.541Z","repository":{"id":39170815,"uuid":"135211802","full_name":"valyala/fastjson","owner":"valyala","description":"Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection","archived":false,"fork":false,"pushed_at":"2024-02-22T21:31:44.000Z","size":847,"stargazers_count":2365,"open_issues_count":61,"forks_count":141,"subscribers_count":26,"default_branch":"master","last_synced_at":"2025-04-24T02:00:42.762Z","etag":null,"topics":["fast","go","golang","json","json-parser","json-validation"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/valyala.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-05-28T21:41:47.000Z","updated_at":"2025-04-20T13:35:01.000Z","dependencies_parsed_at":"2024-06-18T11:07:45.420Z","dependency_job_id":"ae3695b0-44e4-416f-bdbc-1b1f752c582e","html_url":"https://github.com/valyala/fastjson","commit_stats":{"total_commits":183,"total_committers":7,"mean_commits":"26.142857142857142","dds":0.04371584699453557,"last_synced_commit":"93f67d942133e9d219dbbae7a541433f8bbbe7c4"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valyala%2Ffastjson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valyala%2Ffastjson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valyala%2Ffastjson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valyala%2Ffastjson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/valyala","download_url":"https://codeload.github.com/valyala/fastjson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253990468,"owners_count":21995774,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fast","go","golang","json","json-parser","json-validation"],"created_at":"2024-07-30T20:01:36.114Z","updated_at":"2025-05-13T17:09:41.368Z","avatar_url":"https://github.com/valyala.png","language":"Go","readme":"[![Build Status](https://travis-ci.org/valyala/fastjson.svg)](https://travis-ci.org/valyala/fastjson)\n[![GoDoc](https://godoc.org/github.com/valyala/fastjson?status.svg)](http://godoc.org/github.com/valyala/fastjson)\n[![Go Report](https://goreportcard.com/badge/github.com/valyala/fastjson)](https://goreportcard.com/report/github.com/valyala/fastjson)\n[![codecov](https://codecov.io/gh/valyala/fastjson/branch/master/graph/badge.svg)](https://codecov.io/gh/valyala/fastjson)\n\n# fastjson - fast JSON parser and validator for Go\n\n\n## Features\n\n  * Fast. As usual, up to 15x faster than the standard [encoding/json](https://golang.org/pkg/encoding/json/).\n    See [benchmarks](#benchmarks).\n  * Parses arbitrary JSON without schema, reflection, struct magic and code generation\n    contrary to [easyjson](https://github.com/mailru/easyjson).\n  * Provides simple [API](http://godoc.org/github.com/valyala/fastjson).\n  * Outperforms [jsonparser](https://github.com/buger/jsonparser) and [gjson](https://github.com/tidwall/gjson)\n    when accessing multiple unrelated fields, since `fastjson` parses the input JSON only once.\n  * Validates the parsed JSON unlike [jsonparser](https://github.com/buger/jsonparser)\n    and [gjson](https://github.com/tidwall/gjson).\n  * May quickly extract a part of the original JSON with `Value.Get(...).MarshalTo` and modify it\n    with [Del](https://godoc.org/github.com/valyala/fastjson#Value.Del)\n    and [Set](https://godoc.org/github.com/valyala/fastjson#Value.Set) functions.\n  * May parse array containing values with distinct types (aka non-homogenous types).\n    For instance, `fastjson` easily parses the following JSON array `[123, \"foo\", [456], {\"k\": \"v\"}, null]`.\n  * `fastjson` preserves the original order of object items when calling\n    [Object.Visit](https://godoc.org/github.com/valyala/fastjson#Object.Visit).\n\n\n## Known limitations\n\n  * Requies extra care to work with - references to certain objects recursively\n    returned by [Parser](https://godoc.org/github.com/valyala/fastjson#Parser)\n    must be released before the next call to [Parse](https://godoc.org/github.com/valyala/fastjson#Parser.Parse).\n    Otherwise the program may work improperly. The same applies to objects returned by [Arena](https://godoc.org/github.com/valyala/fastjson#Arena).\n    Adhere recommendations from [docs](https://godoc.org/github.com/valyala/fastjson).\n  * Cannot parse JSON from `io.Reader`. There is [Scanner](https://godoc.org/github.com/valyala/fastjson#Scanner)\n    for parsing stream of JSON values from a string.\n\n\n## Usage\n\nOne-liner accessing a single field:\n```go\n\ts := []byte(`{\"foo\": [123, \"bar\"]}`)\n\tfmt.Printf(\"foo.0=%d\\n\", fastjson.GetInt(s, \"foo\", \"0\"))\n\n\t// Output:\n\t// foo.0=123\n```\n\nAccessing multiple fields with error handling:\n```go\n        var p fastjson.Parser\n        v, err := p.Parse(`{\n                \"str\": \"bar\",\n                \"int\": 123,\n                \"float\": 1.23,\n                \"bool\": true,\n                \"arr\": [1, \"foo\", {}]\n        }`)\n        if err != nil {\n                log.Fatal(err)\n        }\n        fmt.Printf(\"foo=%s\\n\", v.GetStringBytes(\"str\"))\n        fmt.Printf(\"int=%d\\n\", v.GetInt(\"int\"))\n        fmt.Printf(\"float=%f\\n\", v.GetFloat64(\"float\"))\n        fmt.Printf(\"bool=%v\\n\", v.GetBool(\"bool\"))\n        fmt.Printf(\"arr.1=%s\\n\", v.GetStringBytes(\"arr\", \"1\"))\n\n        // Output:\n        // foo=bar\n        // int=123\n        // float=1.230000\n        // bool=true\n        // arr.1=foo\n```\n\nSee also [examples](https://godoc.org/github.com/valyala/fastjson#pkg-examples).\n\n\n## Security\n\n  * `fastjson` shouldn't crash or panic when parsing input strings specially crafted\n    by an attacker. It must return error on invalid input JSON.\n  * `fastjson` requires up to `sizeof(Value) * len(inputJSON)` bytes of memory\n    for parsing `inputJSON` string. Limit the maximum size of the `inputJSON`\n    before parsing it in order to limit the maximum memory usage.\n\n\n## Performance optimization tips\n\n  * Re-use [Parser](https://godoc.org/github.com/valyala/fastjson#Parser) and [Scanner](https://godoc.org/github.com/valyala/fastjson#Scanner)\n    for parsing many JSONs. This reduces memory allocations overhead.\n    [ParserPool](https://godoc.org/github.com/valyala/fastjson#ParserPool) may be useful in this case.\n  * Prefer calling `Value.Get*` on the value returned from [Parser](https://godoc.org/github.com/valyala/fastjson#Parser)\n    instead of calling `Get*` one-liners when multiple fields\n    must be obtained from JSON, since each `Get*` one-liner re-parses\n    the input JSON again.\n  * Prefer calling once [Value.Get](https://godoc.org/github.com/valyala/fastjson#Value.Get)\n    for common prefix paths and then calling `Value.Get*` on the returned value\n    for distinct suffix paths.\n  * Prefer iterating over array returned from [Value.GetArray](https://godoc.org/github.com/valyala/fastjson#Object.Visit)\n    with a range loop instead of calling `Value.Get*` for each array item.\n\n## Fuzzing\nInstall [go-fuzz](https://github.com/dvyukov/go-fuzz) \u0026 optionally the go-fuzz-corpus.\n\n```bash\ngo get -u github.com/dvyukov/go-fuzz/go-fuzz github.com/dvyukov/go-fuzz/go-fuzz-build\n```\n\nBuild using `go-fuzz-build` and run `go-fuzz` with an optional corpus.\n\n```bash\nmkdir -p workdir/corpus\ncp $GOPATH/src/github.com/dvyukov/go-fuzz-corpus/json/corpus/* workdir/corpus\ngo-fuzz-build github.com/valyala/fastjson\ngo-fuzz -bin=fastjson-fuzz.zip -workdir=workdir\n```\n\n## Benchmarks\n\nGo 1.12 has been used for benchmarking.\n\nLegend:\n\n  * `small` - parse [small.json](testdata/small.json) (190 bytes).\n  * `medium` - parse [medium.json](testdata/medium.json) (2.3KB).\n  * `large` - parse [large.json](testdata/large.json) (28KB).\n  * `canada` - parse [canada.json](testdata/canada.json) (2.2MB).\n  * `citm` - parse [citm_catalog.json](testdata/citm_catalog.json) (1.7MB).\n  * `twitter` - parse [twitter.json](testdata/twitter.json) (617KB).\n\n  * `stdjson-map` - parse into a `map[string]interface{}` using `encoding/json`.\n  * `stdjson-struct` - parse into a struct containing\n    a subset of fields of the parsed JSON, using `encoding/json`.\n  * `stdjson-empty-struct` - parse into an empty struct using `encoding/json`.\n    This is the fastest possible solution for `encoding/json`, may be used\n    for json validation. See also benchmark results for json validation.\n  * `fastjson` - parse using `fastjson` without fields access.\n  * `fastjson-get` - parse using `fastjson` with fields access similar to `stdjson-struct`.\n\n```\n$ GOMAXPROCS=1 go test github.com/valyala/fastjson -bench='Parse$'\ngoos: linux\ngoarch: amd64\npkg: github.com/valyala/fastjson\nBenchmarkParse/small/stdjson-map         \t  200000\t      7305 ns/op\t  26.01 MB/s\t     960 B/op\t      51 allocs/op\nBenchmarkParse/small/stdjson-struct      \t  500000\t      3431 ns/op\t  55.37 MB/s\t     224 B/op\t       4 allocs/op\nBenchmarkParse/small/stdjson-empty-struct         \t  500000\t      2273 ns/op\t  83.58 MB/s\t     168 B/op\t       2 allocs/op\nBenchmarkParse/small/fastjson                     \t 5000000\t       347 ns/op\t 547.53 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkParse/small/fastjson-get                 \t 2000000\t       620 ns/op\t 306.39 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkParse/medium/stdjson-map                 \t   30000\t     40672 ns/op\t  57.26 MB/s\t   10196 B/op\t     208 allocs/op\nBenchmarkParse/medium/stdjson-struct              \t   30000\t     47792 ns/op\t  48.73 MB/s\t    9174 B/op\t     258 allocs/op\nBenchmarkParse/medium/stdjson-empty-struct        \t  100000\t     22096 ns/op\t 105.40 MB/s\t     280 B/op\t       5 allocs/op\nBenchmarkParse/medium/fastjson                    \t  500000\t      3025 ns/op\t 769.90 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkParse/medium/fastjson-get                \t  500000\t      3211 ns/op\t 725.20 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkParse/large/stdjson-map                  \t    2000\t    614079 ns/op\t  45.79 MB/s\t  210734 B/op\t    2785 allocs/op\nBenchmarkParse/large/stdjson-struct               \t    5000\t    298554 ns/op\t  94.18 MB/s\t   15616 B/op\t     353 allocs/op\nBenchmarkParse/large/stdjson-empty-struct         \t    5000\t    268577 ns/op\t 104.69 MB/s\t     280 B/op\t       5 allocs/op\nBenchmarkParse/large/fastjson                     \t   50000\t     35210 ns/op\t 798.56 MB/s\t       5 B/op\t       0 allocs/op\nBenchmarkParse/large/fastjson-get                 \t   50000\t     35171 ns/op\t 799.46 MB/s\t       5 B/op\t       0 allocs/op\nBenchmarkParse/canada/stdjson-map                 \t      20\t  68147307 ns/op\t  33.03 MB/s\t12260502 B/op\t  392539 allocs/op\nBenchmarkParse/canada/stdjson-struct              \t      20\t  68044518 ns/op\t  33.08 MB/s\t12260123 B/op\t  392534 allocs/op\nBenchmarkParse/canada/stdjson-empty-struct        \t     100\t  17709250 ns/op\t 127.11 MB/s\t     280 B/op\t       5 allocs/op\nBenchmarkParse/canada/fastjson                    \t     300\t   4182404 ns/op\t 538.22 MB/s\t  254902 B/op\t     381 allocs/op\nBenchmarkParse/canada/fastjson-get                \t     300\t   4274744 ns/op\t 526.60 MB/s\t  254902 B/op\t     381 allocs/op\nBenchmarkParse/citm/stdjson-map                   \t      50\t  27772612 ns/op\t  62.19 MB/s\t 5214163 B/op\t   95402 allocs/op\nBenchmarkParse/citm/stdjson-struct                \t     100\t  14936191 ns/op\t 115.64 MB/s\t    1989 B/op\t      75 allocs/op\nBenchmarkParse/citm/stdjson-empty-struct          \t     100\t  14946034 ns/op\t 115.56 MB/s\t     280 B/op\t       5 allocs/op\nBenchmarkParse/citm/fastjson                      \t    1000\t   1879714 ns/op\t 918.87 MB/s\t   17628 B/op\t      30 allocs/op\nBenchmarkParse/citm/fastjson-get                  \t    1000\t   1881598 ns/op\t 917.94 MB/s\t   17628 B/op\t      30 allocs/op\nBenchmarkParse/twitter/stdjson-map                \t     100\t  11289146 ns/op\t  55.94 MB/s\t 2187878 B/op\t   31266 allocs/op\nBenchmarkParse/twitter/stdjson-struct             \t     300\t   5779442 ns/op\t 109.27 MB/s\t     408 B/op\t       6 allocs/op\nBenchmarkParse/twitter/stdjson-empty-struct       \t     300\t   5738504 ns/op\t 110.05 MB/s\t     408 B/op\t       6 allocs/op\nBenchmarkParse/twitter/fastjson                   \t    2000\t    774042 ns/op\t 815.86 MB/s\t    2541 B/op\t       2 allocs/op\nBenchmarkParse/twitter/fastjson-get               \t    2000\t    777833 ns/op\t 811.89 MB/s\t    2541 B/op\t       2 allocs/op\n```\n\nBenchmark results for json validation:\n\n```\n$ GOMAXPROCS=1 go test github.com/valyala/fastjson -bench='Validate$'\ngoos: linux\ngoarch: amd64\npkg: github.com/valyala/fastjson\nBenchmarkValidate/small/stdjson \t 2000000\t       955 ns/op\t 198.83 MB/s\t      72 B/op\t       2 allocs/op\nBenchmarkValidate/small/fastjson         \t 5000000\t       384 ns/op\t 493.60 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkValidate/medium/stdjson         \t  200000\t     10799 ns/op\t 215.66 MB/s\t     184 B/op\t       5 allocs/op\nBenchmarkValidate/medium/fastjson        \t  300000\t      3809 ns/op\t 611.30 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkValidate/large/stdjson          \t   10000\t    133064 ns/op\t 211.31 MB/s\t     184 B/op\t       5 allocs/op\nBenchmarkValidate/large/fastjson         \t   30000\t     45268 ns/op\t 621.14 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkValidate/canada/stdjson         \t     200\t   8470904 ns/op\t 265.74 MB/s\t     184 B/op\t       5 allocs/op\nBenchmarkValidate/canada/fastjson        \t     500\t   2973377 ns/op\t 757.07 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkValidate/citm/stdjson           \t     200\t   7273172 ns/op\t 237.48 MB/s\t     184 B/op\t       5 allocs/op\nBenchmarkValidate/citm/fastjson          \t    1000\t   1684430 ns/op\t1025.39 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkValidate/twitter/stdjson        \t     500\t   2849439 ns/op\t 221.63 MB/s\t     312 B/op\t       6 allocs/op\nBenchmarkValidate/twitter/fastjson       \t    2000\t   1036796 ns/op\t 609.10 MB/s\t       0 B/op\t       0 allocs/op\n```\n\n## FAQ\n\n  * Q: _There are a ton of other high-perf packages for JSON parsing in Go. Why creating yet another package?_\n    A: Because other packages require either rigid JSON schema via struct magic\n       and code generation or perform poorly when multiple unrelated fields\n       must be obtained from the parsed JSON.\n       Additionally, `fastjson` provides nicer [API](http://godoc.org/github.com/valyala/fastjson).\n\n  * Q: _What is the main purpose for `fastjson`?_\n    A: High-perf JSON parsing for [RTB](https://www.iab.com/wp-content/uploads/2015/05/OpenRTB_API_Specification_Version_2_3_1.pdf)\n       and other [JSON-RPC](https://en.wikipedia.org/wiki/JSON-RPC) services.\n\n  * Q: _Why fastjson doesn't provide fast marshaling (serialization)?_\n    A: Actually it provides some sort of marshaling - see [Value.MarshalTo](https://godoc.org/github.com/valyala/fastjson#Value.MarshalTo).\n       But I'd recommend using [quicktemplate](https://github.com/valyala/quicktemplate#use-cases)\n       for high-performance JSON marshaling :)\n\n  * Q: _`fastjson` crashes my program!_\n    A: There is high probability of improper use.\n       * Make sure you don't hold references to objects recursively returned by `Parser` / `Scanner`\n         beyond the next `Parser.Parse` / `Scanner.Next` call\n         if such restriction is mentioned in [docs](https://github.com/valyala/fastjson/issues/new).\n       * Make sure you don't access `fastjson` objects from concurrently running goroutines\n         if such restriction is mentioned in [docs](https://github.com/valyala/fastjson/issues/new).\n       * Build and run your program with [-race](https://golang.org/doc/articles/race_detector.html) flag.\n         Make sure the race detector detects zero races.\n       * If your program continue crashing after fixing issues mentioned above, [file a bug](https://github.com/valyala/fastjson/issues/new).\n","funding_links":[],"categories":["开源类库","Go","JSON","JSON parsers \u0026 validators","Open source library","0x04. valyala/fastjson","Relational Databases","Repositories"],"sub_categories":["JSON","检索及分析资料库","Search and Analytic Databases","Advanced Console UIs"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalyala%2Ffastjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvalyala%2Ffastjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalyala%2Ffastjson/lists"}