Simple Clojure(Script) library for working with JSON Pointer and JSON Patch, with no external dependencies. The JSON Patch function provided passes all the tests from the JSON patch conformance test suite.
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
:
(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
:
(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:
(jp/apply-patch {} {"op" "add" "path" "/a" "value" 1}) ; => {"a" 1}
; or, more likely:
(reduce jp/apply-patch {} patches)
clj -X:test
to run the unit and compliance testsshadow-cljs compile test && node target/cljs-test.js
for ClojureScriptclj -T:build jar
clj -X:deploy
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close