jsonmerge

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2019 License: MIT Imports: 6 Imported by: 7

README

go-jsonmerge

Build Status GoDoc

GO library for merging JSON objects

Original document

{  
  "number": 1,
  "string": "value",
  "object": {
    "number": 1,
    "string": "value",
    "nested object": {
      "number": 2
    },
    "array": [1, 2, 3],
    "partial_array": [1, 2, 3]
  }
}

Patch

{  
  "number": 2,
  "string": "value1",
  "nonexitent": "woot",
  "object": {
    "number": 3,
    "string": "value2",
    "nested object": {
      "number": 4
    },
    "array": [3, 2, 1],
    "partial_array": {
      "1": 4
    }
  }
}

Result

{  
  "number": 2,
  "string": "value1",
  "object": {
    "number": 3,
    "string": "value2",
    "nested object": {
      "number": 4
    },
    "array": [3, 2, 1],
    "partial_array": [1, 4, 3]
  }
}

Commandline Tool

$ go get -u github.com/RaveNoX/go-jsonmerge/cmd/jsonmerge
$ jsonmerge [options] <patch.json> <glob1.json> <glob2.json>...<globN.json>
# For help
$ jsonmerge -h

Development

# Install depencencies
./init.sh

# Build
./build.sh

License

MIT

Documentation

Overview

Package jsonmerge helps mergeing JSON objects

For example you have this documents:

original.json

{
  "number": 1,
  "string": "value",
  "object": {
    "number": 1,
      "string": "value",
      "nested object": {
        "number": 2
      },
      "array": [1, 2, 3],
      "partial_array": [1, 2, 3]
   }
}

patch.json

{
  "number": 2,
  "string": "value1",
  "nonexitent": "woot",
  "object": {
    "number": 3,
    "string": "value2",
    "nested object": {
      "number": 4
    },
    "array": [3, 2, 1],
    "partial_array": {
      "1": 4
    }
  }
}

After merge you will have this result:

{
  "number": 2,
  "string": "value1",
  "object": {
    "number": 3,
    "string": "value2",
    "nested object": {
      "number": 4
    },
    "array": [3, 2, 1],
    "partial_array": [1, 4, 3]
  }
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Info

type Info struct {
	// Errors is slice of non-critical errors of merge operations
	Errors []error
	// Replaced is describe replacements
	// Key is path in document like
	//   "prop1.prop2.prop3" for object properties or
	//   "arr1.1.prop" for arrays
	// Value is value of replacemet
	Replaced map[string]interface{}
}

Info describes result of merge operation

func Merge

func Merge(data, patch interface{}) (interface{}, *Info)

Merge merges patch document to data document

Returning merged document and merge info

func MergeBytes

func MergeBytes(dataBuff, patchBuff []byte) (mergedBuff []byte, info *Info, err error)

MergeBytes merges patch document buffer to data document buffer

Returning merged document buffer, merge info and error if any

func MergeBytesIndent

func MergeBytesIndent(dataBuff, patchBuff []byte, prefix, indent string) (mergedBuff []byte, info *Info, err error)

MergeBytesIndent merges patch document buffer to data document buffer

Use prefix and indent for set indentation like in json.MarshalIndent

Returning merged document buffer, merge info and error if any

Directories

Path Synopsis
cmd
JSON merging utility Usage: jsomerge [-q|-v] <patch.json> <original_glob1>..<original_globN>
JSON merging utility Usage: jsomerge [-q|-v] <patch.json> <original_glob1>..<original_globN>

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL