{"id":15680652,"url":"https://github.com/chanced/jsonpointer","last_synced_at":"2025-05-07T11:17:00.117Z","repository":{"id":45062989,"uuid":"444227478","full_name":"chanced/jsonpointer","owner":"chanced","description":"JSON Pointers (rfc 6901) for Go","archived":false,"fork":false,"pushed_at":"2022-09-10T19:42:42.000Z","size":195,"stargazers_count":11,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-05-07T11:16:54.278Z","etag":null,"topics":["go","golang","json","json-pointer","rfc-6901","rfc6901"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chanced.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}},"created_at":"2022-01-03T23:28:08.000Z","updated_at":"2024-02-10T21:52:25.000Z","dependencies_parsed_at":"2022-09-08T23:51:04.417Z","dependency_job_id":null,"html_url":"https://github.com/chanced/jsonpointer","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chanced%2Fjsonpointer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chanced%2Fjsonpointer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chanced%2Fjsonpointer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chanced%2Fjsonpointer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chanced","download_url":"https://codeload.github.com/chanced/jsonpointer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252865589,"owners_count":21816309,"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":["go","golang","json","json-pointer","rfc-6901","rfc6901"],"created_at":"2024-10-03T16:43:47.898Z","updated_at":"2025-05-07T11:17:00.097Z","avatar_url":"https://github.com/chanced.png","language":"Go","readme":"# jsonpointer - an [RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901) implementation for Go\n\n[![GoDoc](https://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square)](https://pkg.go.dev/github.com/chanced/jsonpointer)\n\nPackage jsonpointer provides the ability to resolve, assign, and delete values\nof any type, including raw JSON, by [JSON\nPointers](https://datatracker.ietf.org/doc/html/rfc6901).\n\n## Install\n\n```bash\ngo get github.com/chanced/jsonpointer\n```\n\n## Usage\n\n### General\n\n```go\npackage main\n\nimport (\n    \"log\"\n    \"encoding/json\"\n    \"github.com/chanced/jsonpointer\"\n)\n\ntype Nested struct {\n    Str string\n}\n\ntype Root struct {\n    Nested Nested\n}\n\nfunc main() {\n\n    r := Root{ Nested: Nested{ Str: \"nested str\" }}\n\n    // jsonpointer.Pointer is a string type so if you have a properly\n    // formatted json pointer then you can simply convert it:\n    //  ptr := jsonpointer.Pointer(myPointer)\n    //  err := ptr.Validate()\n\n    // Note: jsonpointer.New encodes each token's value.\n    // \"/\" encodes to \"~1\" and \"~\" encodes to \"~0\" in compliance with RFC 6901.\n\n    ptr := jsonpointer.New(\"nested\", \"str\")\n\n    // Resolve\n\n    var s string\n    err := jsonpointer.Resolve(r, ptr, \u0026s)\n    if err != nil {\n        log.Fatal(err)\n    }\n    log.Println(s) // outputs \"nested str\"\n\n    // Assign\n\n    err = jsonpointer.Assign(\u0026r, ptr, \"new value\")\n    if err != nil {\n        log.Fatal(err)\n    }\n    log.Println(r.Nested.Str) // outputs \"new value\"\n\n\n    // Delete\n\n    err = jsonpointer.Delete(\u0026r, ptr)\n    if err != nil {\n        log.Fatal(err)\n    }\n    log.Println(r.Nested.Str) // outputs \"\"\n\n\n    // jsonpointer can also Resolve, Assign, and Delete JSON in []byte format.\n    // This includes field values, such as those of type json.RawMessage.\n\n    r.Nested.Str = \"str val\"\n\n    b, err := json.Marshal(r)\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    err = jsonpointer.Resolve(b, ptr, \u0026s)\n    if err != nil {\n        log.Fatal(err)\n    }\n    log.Println(s) // outputs \"str val\"\n}\n```\n\n### Interfaces\n\nPackage jsonpointer provides 3 interfaces: `Assigner`, `Resolver`, and\n`Deleter`. Regardless of the operation, if `Resolver` is implemented, `ResolvePointer` will be\ncalled. `ResolvePointer` should not have side effects. If resolving for an assignment, utilize the\npointer to infer which type should be assigned.\n\n`AssignByPointer` is invoked on the way back from the leaf. `DeleteByPointer` is invoked after resolving the current token.\n\nAll three methods are passed a pointer to the `jsonpointer.Pointer` so that\nit can be modified. If you do not modify it, jsonpointer will assume the current\ntoken was addressed and continue on.\n\nIf you wish to only handle some cases with the interfaces, return `jsonpointer.YieldOperation` to have the jsonpointer package resolve, assign, or delete as if the type did not implement the interface. Note that doing so results in changes to `ptr` being dismissed.\n\n### Pointer methods\n\nAll methods return new values rather than modifying the pointer itself. If you wish to modify the pointer in one of the interface methods, you will need to reassign it: `*ptr = newPtrVal`\n\n```go\nfunc (mt MyType) ResolvePointer(ptr *jsonpointer.Pointer, op Operation) (interface{}, error) {\n    next, t, ok := ptr.Next()\n    if !ok {\n        // this will only occur if the ptr is a root token in this circumstance\n        return mt\n    }\n    if op == jsonpointer.Assigning \u0026\u0026 t == \"someInterface\" {\n        // maybe you need to know what comes after someInterface to\n        // determine what implementation of someInterface to assign\n        t, _ = next.NextToken()\n\n        switch t {\n        case \"someIdentifier\":\n            // you could modify ptr if you felt so inclined: *ptr = next\n            // but it is not needed in this scenario.\n            return SomeImplementation{}, nil\n        }\n    }\n    // otherwise hand resolution back over to jsonpointer\n    return nil, jsonpointer.YieldOperation\n}\n```\n\n## Errors\n\nAll errors returned from `Resolve`, `Assign`, and `Delete` will implement `Error`. A convenience function `AsError` exists to help extract out the details.\n\nDepending on the cause, the error could also be `KeyError`, `IndexError`, `FieldError` with additional details. All have corresponding `As{Error}` functions.\n\nFinally, all errors have associated Err instances that are wrapped, such as `ErrMalformedToken`, `ErrInvalidKeyType`, and so on.\n\nSee [errors.go for further details on errors](https://github.com/chanced/jsonpointer/blob/main/errors.go).\n\n## Contributions \u0026 Issues\n\nContributions are always welcome. If you run into an issue, please open a issue\non github. If you would like to submit a change, feel free to open up a pull\nrequest.\n\n## Note on Performance\n\nThis package is reflect heavy. While it employs the same caching mechanics as\n`encoding/json` to help alleviate some of the lookup costs, there will always be\na performance hit with reflection.\n\nThere are probably plenty of ways to improve performance of the package.\nImprovements or criticisms are always welcome.\n\nWith regards to raw JSON, `json.Marshal` and `json.Unmarshal` are utilized.\nIdeally, in the future, that will change and the package will incoroprate the\nencoding/decoding logic from `encoding/json` directly, thus skipping the need to\nrun through unneccesary logic.\n\n## Alternative JSON Pointer Packages for Go\n\n-   [github.com/dolmen-go/jsonptr](https://github.com/dolmen-go/jsonptr)\n-   [github.com/qri-io/jsonpointer](https://github.com/qri-io/jsonpointer)\n-   [github.com/xeipuuv/gojsonpointer](https://github.com/xeipuuv/gojsonpointer)\n-   [github.com/go-openapi/jsonpointer](https://github.com/go-openapi/jsonpointer)\n\n## License\n\n[Apache 2.0](https://raw.githubusercontent.com/chanced/jsonpointer/main/LICENSE)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchanced%2Fjsonpointer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchanced%2Fjsonpointer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchanced%2Fjsonpointer/lists"}