jsonpatch: JSON Patch parsing and application

[ agpl, json, library ] [ Propose Tags ] [ Report a vulnerability ]

This is a Haskell library for parsing and applying JSON Patches

From https://www.json.org/:

JSON Patch is a format for describing changes to a JSON document. It can be used to avoid sending a whole document when only a part has changed. When used in combination with the HTTP PATCH method, it allows partial updates for HTTP APIs in a standards compliant way.

The patch documents are themselves JSON documents.

JSON Patch is specified in RFC 6902 from the IETF.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0.0, 0.2.0.0, 0.2.1.0, 0.2.2.0, 0.3.0.0, 0.3.0.1 (info)
Change log CHANGELOG.md
Dependencies aeson (>=2.0.3.0), aeson-optics (>=1.2.0.1), base (>=4.16.4.0 && <5), bytestring (>=0.11.4.0), optics-core (>=0.4.1), text (>=1.2.5.0), vector (>=0.12.3.1) [details]
License AGPL-3.0-only
Author
Maintainer Patrick Brisbin
Category JSON
Home page https://github.com/pbrisbin/jsonpatch#readme
Bug tracker https://github.com/pbrisbin/jsonpatch/issues
Source repo head: git clone https://github.com/pbrisbin/jsonpatch
Uploaded by PatrickBrisbin at 2025-04-11T01:06:18Z
Distributions
Downloads 9 total (9 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2025-04-11 [all 1 reports]

Readme for jsonpatch-0.3.0.1

[back to package description]

jsonpatch

Hackage Stackage Nightly Stackage LTS CI

Haskell package for parsing and applying JSON Patches.

Example

Typical use cases need only one import:

import Data.JSON.Patch

Our example will make use of a few more libraries:

import Control.Exception (displayException)
import Data.Aeson (FromJSON, ToJSON, Result(..), Value, fromJSON)
import Data.Aeson.Encode.Pretty
import Data.Aeson.QQ (aesonQQ)
import Data.ByteString (ByteString)
import Data.ByteString.Lazy qualified as BSL
import Data.Text (Text)
import GHC.Generics (Generic)

The FromJSON instance can be used to build a [Patch]:

patch :: [Patch]
patch = fromResult $ fromJSON [aesonQQ|
  [
    { "op": "replace", "path": "/baz", "value": "boo" },
    { "op": "add", "path": "/hello", "value": ["world"] },
    { "op": "remove", "path": "/foo" }
  ]
|]


-- | Unsafe unwrapping for the sake of example
fromResult :: Result a -> a
fromResult (Success a) = a

The patches can then be applied to a document:

result :: Either PatchError Value
result = patchValue patch [aesonQQ|
  {
    "baz": "qux",
    "foo": "bar"
  }
|]

The result is in Either PatchError, with displayException available to get a user-friendly message.

main :: IO ()
main = either (fail . displayException) (BSL.putStr . encodePretty) result

The above program outputs:

{
  "baz": "boo",
  "hello": ["world"]
}

AsValue Example

The polymorphic patchAsValue function is also available, which provides the following benefits over patchValue:

  1. The patches argument can be any AsValue (from aeson-optics), meaning you can give it directly a ByteString, Value, or Text. Parse errors turning it into [Patch] will be normalized to PatchError.
  2. The target argument can be any type with FromJSON and ToJSON. This means you can patch any of your domain types directly. AsValue would've worked here too, but your domain types are far less likely to have that instance.
data Dog = Dog
  { name :: Text
  , isGood :: Bool
  }
  deriving stock Generic
  deriving anyclass (FromJSON, ToJSON)

fido :: Dog
fido = Dog "fido" False -- gasp!

bytes :: ByteString
bytes = "[{ \"op\":\"replace\", \"path\":\"/isGood\", \"value\":true }]"

result2 :: Either PatchError Dog
result2 = patchAsValue bytes fido
main :: IO ()
main = either (fail . displayException) (BSL.putStr . encodePretty) result2

The above program outputs:

{
  "isGood": true,
  "name": "fido"
}

Quality

The full test suite from json-patch/json-patch-tests passes. However, some error cases have poor (or misleading) error messages at this time.

License

This package is licensed AGPLv3. See COPYING.