{"id":13581788,"url":"https://github.com/tidwall/sjson","last_synced_at":"2025-05-13T21:04:13.962Z","repository":{"id":37385122,"uuid":"71305027","full_name":"tidwall/sjson","owner":"tidwall","description":"Set JSON values very quickly in Go","archived":false,"fork":false,"pushed_at":"2024-04-23T11:25:31.000Z","size":94,"stargazers_count":2547,"open_issues_count":39,"forks_count":172,"subscribers_count":29,"default_branch":"master","last_synced_at":"2025-05-09T20:08:34.303Z","etag":null,"topics":["json"],"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/tidwall.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":"2016-10-19T01:01:04.000Z","updated_at":"2025-05-07T12:00:09.000Z","dependencies_parsed_at":"2022-07-08T19:01:55.647Z","dependency_job_id":"ce7c4e94-8e74-4a4d-89e7-3b7997ffb5bd","html_url":"https://github.com/tidwall/sjson","commit_stats":{"total_commits":53,"total_committers":11,"mean_commits":4.818181818181818,"dds":0.2264150943396226,"last_synced_commit":"b279807a1bad9fa155988667d97a3d2fff4061b5"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidwall%2Fsjson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidwall%2Fsjson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidwall%2Fsjson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidwall%2Fsjson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tidwall","download_url":"https://codeload.github.com/tidwall/sjson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253377131,"owners_count":21898934,"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":["json"],"created_at":"2024-08-01T15:02:14.616Z","updated_at":"2025-05-13T21:04:13.939Z","avatar_url":"https://github.com/tidwall.png","language":"Go","readme":"\u003cp align=\"center\"\u003e\n\u003cimg \n    src=\"logo.png\" \n    width=\"240\" height=\"78\" border=\"0\" alt=\"SJSON\"\u003e\n\u003cbr\u003e\n\u003ca href=\"https://godoc.org/github.com/tidwall/sjson\"\u003e\u003cimg src=\"https://img.shields.io/badge/api-reference-blue.svg?style=flat-square\" alt=\"GoDoc\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003eset a json value quickly\u003c/p\u003e\n\nSJSON is a Go package that provides a [very fast](#performance) and simple way to set a value in a json document.\nFor quickly retrieving json values check out [GJSON](https://github.com/tidwall/gjson).\n\nFor a command line interface check out [JJ](https://github.com/tidwall/jj).\n\nGetting Started\n===============\n\nInstalling\n----------\n\nTo start using SJSON, install Go and run `go get`:\n\n```sh\n$ go get -u github.com/tidwall/sjson\n```\n\nThis will retrieve the library.\n\nSet a value\n-----------\nSet sets the value for the specified path. \nA path is in dot syntax, such as \"name.last\" or \"age\". \nThis function expects that the json is well-formed and validated. \nInvalid json will not panic, but it may return back unexpected results.\nInvalid paths may return an error.\n\n```go\npackage main\n\nimport \"github.com/tidwall/sjson\"\n\nconst json = `{\"name\":{\"first\":\"Janet\",\"last\":\"Prichard\"},\"age\":47}`\n\nfunc main() {\n\tvalue, _ := sjson.Set(json, \"name.last\", \"Anderson\")\n\tprintln(value)\n}\n```\n\nThis will print:\n\n```json\n{\"name\":{\"first\":\"Janet\",\"last\":\"Anderson\"},\"age\":47}\n```\n\nPath syntax\n-----------\n\nA path is a series of keys separated by a dot.\nThe dot and colon characters can be escaped with ``\\``.\n\n```json\n{\n  \"name\": {\"first\": \"Tom\", \"last\": \"Anderson\"},\n  \"age\":37,\n  \"children\": [\"Sara\",\"Alex\",\"Jack\"],\n  \"fav.movie\": \"Deer Hunter\",\n  \"friends\": [\n\t{\"first\": \"James\", \"last\": \"Murphy\"},\n\t{\"first\": \"Roger\", \"last\": \"Craig\"}\n  ]\n}\n```\n```\n\"name.last\"          \u003e\u003e \"Anderson\"\n\"age\"                \u003e\u003e 37\n\"children.1\"         \u003e\u003e \"Alex\"\n\"friends.1.last\"     \u003e\u003e \"Craig\"\n```\n\nThe `-1` key can be used to append a value to an existing array:\n\n```\n\"children.-1\"  \u003e\u003e appends a new value to the end of the children array\n```\n\nNormally number keys are used to modify arrays, but it's possible to force a numeric object key by using the colon character:\n\n```json\n{\n  \"users\":{\n    \"2313\":{\"name\":\"Sara\"},\n    \"7839\":{\"name\":\"Andy\"}\n  }\n}\n```\n\nA colon path would look like:\n\n```\n\"users.:2313.name\"    \u003e\u003e \"Sara\"\n```\n\nSupported types\n---------------\n\nPretty much any type is supported:\n\n```go\nsjson.Set(`{\"key\":true}`, \"key\", nil)\nsjson.Set(`{\"key\":true}`, \"key\", false)\nsjson.Set(`{\"key\":true}`, \"key\", 1)\nsjson.Set(`{\"key\":true}`, \"key\", 10.5)\nsjson.Set(`{\"key\":true}`, \"key\", \"hello\")\nsjson.Set(`{\"key\":true}`, \"key\", []string{\"hello\", \"world\"})\nsjson.Set(`{\"key\":true}`, \"key\", map[string]interface{}{\"hello\":\"world\"})\n```\n\nWhen a type is not recognized, SJSON will fallback to the `encoding/json` Marshaller.\n\n\nExamples\n--------\n\nSet a value from empty document:\n```go\nvalue, _ := sjson.Set(\"\", \"name\", \"Tom\")\nprintln(value)\n\n// Output:\n// {\"name\":\"Tom\"}\n```\n\nSet a nested value from empty document:\n```go\nvalue, _ := sjson.Set(\"\", \"name.last\", \"Anderson\")\nprintln(value)\n\n// Output:\n// {\"name\":{\"last\":\"Anderson\"}}\n```\n\nSet a new value:\n```go\nvalue, _ := sjson.Set(`{\"name\":{\"last\":\"Anderson\"}}`, \"name.first\", \"Sara\")\nprintln(value)\n\n// Output:\n// {\"name\":{\"first\":\"Sara\",\"last\":\"Anderson\"}}\n```\n\nUpdate an existing value:\n```go\nvalue, _ := sjson.Set(`{\"name\":{\"last\":\"Anderson\"}}`, \"name.last\", \"Smith\")\nprintln(value)\n\n// Output:\n// {\"name\":{\"last\":\"Smith\"}}\n```\n\nSet a new array value:\n```go\nvalue, _ := sjson.Set(`{\"friends\":[\"Andy\",\"Carol\"]}`, \"friends.2\", \"Sara\")\nprintln(value)\n\n// Output:\n// {\"friends\":[\"Andy\",\"Carol\",\"Sara\"]\n```\n\nAppend an array value by using the `-1` key in a path:\n```go\nvalue, _ := sjson.Set(`{\"friends\":[\"Andy\",\"Carol\"]}`, \"friends.-1\", \"Sara\")\nprintln(value)\n\n// Output:\n// {\"friends\":[\"Andy\",\"Carol\",\"Sara\"]\n```\n\nAppend an array value that is past the end:\n```go\nvalue, _ := sjson.Set(`{\"friends\":[\"Andy\",\"Carol\"]}`, \"friends.4\", \"Sara\")\nprintln(value)\n\n// Output:\n// {\"friends\":[\"Andy\",\"Carol\",null,null,\"Sara\"]\n```\n\nDelete a value:\n```go\nvalue, _ := sjson.Delete(`{\"name\":{\"first\":\"Sara\",\"last\":\"Anderson\"}}`, \"name.first\")\nprintln(value)\n\n// Output:\n// {\"name\":{\"last\":\"Anderson\"}}\n```\n\nDelete an array value:\n```go\nvalue, _ := sjson.Delete(`{\"friends\":[\"Andy\",\"Carol\"]}`, \"friends.1\")\nprintln(value)\n\n// Output:\n// {\"friends\":[\"Andy\"]}\n```\n\nDelete the last array value:\n```go\nvalue, _ := sjson.Delete(`{\"friends\":[\"Andy\",\"Carol\"]}`, \"friends.-1\")\nprintln(value)\n\n// Output:\n// {\"friends\":[\"Andy\"]}\n```\n\n## Performance\n\nBenchmarks of SJSON alongside [encoding/json](https://golang.org/pkg/encoding/json/), \n[ffjson](https://github.com/pquerna/ffjson), \n[EasyJSON](https://github.com/mailru/easyjson),\nand [Gabs](https://github.com/Jeffail/gabs)\n\n```\nBenchmark_SJSON-8                  \t 3000000\t       805 ns/op\t    1077 B/op\t       3 allocs/op\nBenchmark_SJSON_ReplaceInPlace-8   \t 3000000\t       449 ns/op\t       0 B/op\t       0 allocs/op\nBenchmark_JSON_Map-8               \t  300000\t     21236 ns/op\t    6392 B/op\t     150 allocs/op\nBenchmark_JSON_Struct-8            \t  300000\t     14691 ns/op\t    1789 B/op\t      24 allocs/op\nBenchmark_Gabs-8                   \t  300000\t     21311 ns/op\t    6752 B/op\t     150 allocs/op\nBenchmark_FFJSON-8                 \t  300000\t     17673 ns/op\t    3589 B/op\t      47 allocs/op\nBenchmark_EasyJSON-8               \t 1500000\t      3119 ns/op\t    1061 B/op\t      13 allocs/op\n```\n\nJSON document used:\n\n```json\n{\n  \"widget\": {\n    \"debug\": \"on\",\n    \"window\": {\n      \"title\": \"Sample Konfabulator Widget\",\n      \"name\": \"main_window\",\n      \"width\": 500,\n      \"height\": 500\n    },\n    \"image\": { \n      \"src\": \"Images/Sun.png\",\n      \"hOffset\": 250,\n      \"vOffset\": 250,\n      \"alignment\": \"center\"\n    },\n    \"text\": {\n      \"data\": \"Click Here\",\n      \"size\": 36,\n      \"style\": \"bold\",\n      \"vOffset\": 100,\n      \"alignment\": \"center\",\n      \"onMouseUp\": \"sun1.opacity = (sun1.opacity / 100) * 90;\"\n    }\n  }\n}    \n```\n\nEach operation was rotated though one of the following search paths:\n\n```\nwidget.window.name\nwidget.image.hOffset\nwidget.text.onMouseUp\n```\n\n*These benchmarks were run on a MacBook Pro 15\" 2.8 GHz Intel Core i7 using Go 1.7 and can be be found [here](https://github.com/tidwall/sjson-benchmarks)*.\n\n## Contact\nJosh Baker [@tidwall](http://twitter.com/tidwall)\n\n## License\n\nSJSON source code is available under the MIT [License](/LICENSE).\n","funding_links":[],"categories":["Misc","开源类库","Go","JSON parsers \u0026 validators","Open source library","JSON"],"sub_categories":["JSON","Search and Analytic Databases","检索及分析资料库"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftidwall%2Fsjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftidwall%2Fsjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftidwall%2Fsjson/lists"}