https://github.com/borgeby/clj-json-pointer
Simple Clojure(Script) library for working with JSON Pointer and JSON Patch, with no external dependencies.
https://github.com/borgeby/clj-json-pointer
clojure clojurescript json-patch json-pointer
Last synced: about 1 month ago
JSON representation
Simple Clojure(Script) library for working with JSON Pointer and JSON Patch, with no external dependencies.
- Host: GitHub
- URL: https://github.com/borgeby/clj-json-pointer
- Owner: borgeby
- License: apache-2.0
- Created: 2022-11-02T10:59:25.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-29T09:08:05.000Z (almost 2 years ago)
- Last Synced: 2025-03-05T22:42:07.952Z (2 months ago)
- Topics: clojure, clojurescript, json-patch, json-pointer
- Language: Clojure
- Homepage:
- Size: 74.2 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cljs-json-pointer
[](https://clojars.org/by.borge/clj-json-pointer)
[](https://cljdoc.org/d/by.borge/clj-json-pointer)

[](https://codecov.io/github/borgeby/clj-json-pointer)Simple Clojure(Script) library for working with [JSON Pointer](https://www.rfc-editor.org/rfc/rfc6901) and
[JSON Patch](https://datatracker.ietf.org/doc/html/rfc6902/), with no external dependencies. The JSON Patch function
provided passes all the tests from the JSON patch [conformance test suite](https://github.com/json-patch/json-patch-tests).## Usage instructions
At the heart of the library is the `->vec` function, which may be used to transform a JSON pointer into a vector
representing the path of an object or array. This vector is suitable for use with the standard Clojure functions for
nested access or updates, like `get-in`, `assoc-in` and `update-in`:```clojure
(ns app
(:require [clj-json-pointer.core :as jp]))(def org
{"department"
{"tech"
{"users"
[{"name" "ted" "roles" ["developer"]}
{"name" "jane" "roles" ["platform" "devops"]}]}
"finance"
{"users"
[{"name" "joe" "roles" ["reports-writer"]}]}}})(let [path (jp/->vec org "/department/tech/users/1/roles") ; => ["department" "tech" 1 "users" "roles"]
roles (get-in org path)] ; => ["platform" "devops"]
(do (something (with roles))))
```These simple building blocks are used to implement the various operations of JSON `patch`:
```clojure
(jp/patch {} ; => {}
[{"op" "add" "path" "/foo" "value" "bar"} ; => {"foo" "bar"}
{"op" "add" "path" "/bar" "value" "baz"} ; => {"foo" "bar" "bar" "baz}
{"op" "remove" "path" "/foo"} ; => {"bar" "baz"}
{"op" "replace" "path" "/bar" "value" "foo"} ; => {"bar" "foo"}
{"op" "copy" "from" "/bar" "path" "/baz"} ; => {"bar" "foo" "baz" "foo"}
{"op" "move" "from" "/baz" "path" "/foo"} ; => {"foo" "foo"}
{"op" "test" "path" "/foo" "value" "foo"}]) ; => {"foo" "foo"}
```Or if you so prefer, use the `apply-patch` function, which applies a single patch to the provided data structure:
```clojure
(jp/apply-patch {} {"op" "add" "path" "/a" "value" 1}) ; => {"a" 1}; or, more likely:
(reduce jp/apply-patch {} patches)
```## Development
### Test
* `clj -X:test` to run the unit and compliance tests
* `shadow-cljs compile test && node target/cljs-test.js` for ClojureScript### Deploy
* `clj -T:build jar`
* `clj -X:deploy`