package module
Version: v0.0.0-...-91ae88d Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2021 License: BSD-3-Clause Imports: 3 Imported by: 1


JSON Merge Patch

A JSON Merge Patch utility forked from json-patch and based on fastjson, with WASM/WASI support for TinyGo.

Wasm Support

The TinyGo compiler doesn't support encoding/json or some of the reflect package that most JSON serializers are based on.

Luckily valyala/fastjson is a native go implementation that doesn't rely on encoding/json and only uses the supported reflect functions.

Therefore this package is a TinyGo compliant library for JSON Merge Patch.


Public Functions:

  • MergePatch(doc, merge) -> doc : Applies a MergePatch to a JSON document and returns the new doc.
  • MergeMergePatch(merge, merge) -> merge : Merges two Merge Patch documents into a single.
package main

import (


func main() {
	// Let's create a merge patch and a original document...
	original := []byte(`{"name": "John", "age": 24, "height": 3.21}`)
	merge := []byte(`{"name": "Jane", "age": 21}`)

	new, err := jsonpatch.MergePatch(original, merge)
	if err != nil {

	fmt.Printf("new merged document:   %s\n", new)
	// outputs {{"name": "Jane", "age": 21, "height": 3.21}}


Currently this project succesfully passes much of the JSON Merge RFC Tests for applying merges to 1. Documents 2. Other Merges.

Next steps is to support creating a Merge from the diff of two existing JSON Docs. This is suppported in the orignal evanphx/json-patch library, but the changes haven't made its way here.


This library is an adaptation of / based on evanphx/json-patch. The goal of this fork is to replace the original libraries dependancy on encoding/json with valyala/fastjson. The original license (BSD 3-Clause) is maintained, and the original copyright is maintained in the unchanged files.





This section is empty.


View Source
var (
	ErrTestFailed   = errors.New("test failed")
	ErrMissing      = errors.New("missing value")
	ErrUnknownType  = errors.New("unknown object type")
	ErrInvalid      = errors.New("invalid state detected")
	ErrInvalidIndex = errors.New("invalid index referenced")
View Source
var ErrBadJSONPatch = fmt.Errorf("Invalid JSON Patch")

var ErrBadJSONDoc = fmt.Errorf("Invalid JSON Document")


func MergeMergePatches

func MergeMergePatches(patch1Data, patch2Data []byte) ([]byte, error)

MergeMergePatches merges two merge patches together, such that applying this resulting merged merge patch to a document yields the same as merging each merge patch to the document in succession.

func MergePatch

func MergePatch(docData, patchData []byte) ([]byte, error)

MergePatch merges the patchData into the docData.


This section is empty.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL