Skip to content

A Golang library trying to reconcilate JSON Patch with JSONPath format

License

Notifications You must be signed in to change notification settings

denouche/go-json-patch-jsonpath

Repository files navigation

go-json-patch-jsonpath

Build Status GoCover GoDoc

A Golang library implementing JSON Patch (rfc6902) with a digression to use path in JSONPath format (https://www.ietf.org/archive/id/draft-ietf-jsonpath-base-08.txt) instead of JSON Pointer (rfc6901).

Work in progress

This lib is a work in progress, for now only replace and remove operations has been implemented.

Installation

go get github.com/denouche/go-json-patch-jsonpath

Usage

package main

import (
	"fmt"
	jsonpatch "github.com/denouche/go-json-patch-jsonpath"
)

type People struct {
	Name    string    `json:"name"`
	Age     int       `json:"age"`
	Friends []*People `json:"friends"`
}

func NewPeople() *People {
	return &People{}
}

func main() {

	person := &People{
		Name: "Foo",
		Age:  42,
		Friends: []*People{
			{Name: "friend 1", Age: 19},
			{Name: "friend 2", Age: 25},
			{Name: "friend 3", Age: 42},
		},
	}

	r := jsonpatch.PatchRequests[People]{
		Patches: []*jsonpatch.PatchRequest[People]{
			{
				Operation: "replace",
				Path:      "$.name",
				Value:     "Bar",
			},
			{
				Operation: "remove",
				Path:      "$.friends[?(@.age < 20)]",
			},
		},
	}

	patchedPerson, err := r.Apply(person, NewPeople)
	if err != nil {
		fmt.Printf("error: %s\n", err)
	}

	fmt.Printf("name:%s age:%d friends:%d\n", person.Name, person.Age, len(person.Friends))
	// name:Foo age:42 friends:3

	fmt.Printf("name:%s age:%d friends:%d\n", patchedPerson.Name, patchedPerson.Age, len(patchedPerson.Friends))
	// name:Bar age:42 friends:2
}

References

JSON Patch: https://www.rfc-editor.org/rfc/rfc6902

JSONPath:

About

A Golang library trying to reconcilate JSON Patch with JSONPath format

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages