README
¶
Gabs is a small utility for dealing with dynamic or unknown JSON structures in
golang. It's pretty much just a helpful wrapper around the golang
json.Marshal/json.Unmarshal
behaviour and map[string]interface{}
objects.
It does nothing spectacular except for being fabulous.
https://godoc.org/github.com/Jeffail/gabs
Install
go get github.com/Jeffail/gabs
Use
Parsing and searching JSON
jsonParsed, err := gabs.ParseJSON([]byte(`{
"outter":{
"inner":{
"value1":10,
"value2":22
},
"alsoInner":{
"value1":20,
"array1":[
30, 40
]
}
}
}`))
var value float64
var ok bool
value, ok = jsonParsed.Path("outter.inner.value1").Data().(float64)
// value == 10.0, ok == true
value, ok = jsonParsed.Search("outter", "inner", "value1").Data().(float64)
// value == 10.0, ok == true
gObj, err := jsonParsed.JSONPointer("/outter/alsoInner/array1/1")
if err != nil {
panic(err)
}
value, ok = gObj.Data().(float64)
// value == 40.0, ok == true
value, ok = jsonParsed.Path("does.not.exist").Data().(float64)
// value == 0.0, ok == false
exists := jsonParsed.Exists("outter", "inner", "value1")
// exists == true
exists := jsonParsed.ExistsP("does.not.exist")
// exists == false
Iterating objects
jsonParsed, _ := gabs.ParseJSON([]byte(`{"object":{ "first": 1, "second": 2, "third": 3 }}`))
// S is shorthand for Search
children, _ := jsonParsed.S("object").ChildrenMap()
for key, child := range children {
fmt.Printf("key: %v, value: %v\n", key, child.Data().(string))
}
Iterating arrays
jsonParsed, err := gabs.ParseJSON([]byte(`{"array":[ "first", "second", "third" ]}`))
if err != nil {
panic(err)
}
// S is shorthand for Search
children, err := jsonParsed.S("array").Children()
if err != nil {
panic(err)
}
for _, child := range children {
fmt.Println(child.Data().(string))
}
Will print:
first
second
third
Children() will return all children of an array in order. This also works on objects, however, the children will be returned in a random order.
Searching through arrays
If your JSON structure contains arrays you can still search the fields of the objects within the array, this returns a JSON array containing the results for each element.
jsonParsed, err := gabs.ParseJSON([]byte(`{"array":[ {"value":1}, {"value":2}, {"value":3} ]}`))
if err != nil {
panic(err)
}
fmt.Println(jsonParsed.Path("array.value").String())
Will print:
[1,2,3]
Generating JSON
jsonObj := gabs.New()
// or gabs.Consume(jsonObject) to work on an existing map[string]interface{}
jsonObj.Set(10, "outter", "inner", "value")
jsonObj.SetP(20, "outter.inner.value2")
jsonObj.Set(30, "outter", "inner2", "value3")
fmt.Println(jsonObj.String())
Will print:
{"outter":{"inner":{"value":10,"value2":20},"inner2":{"value3":30}}}
To pretty-print:
fmt.Println(jsonObj.StringIndent("", " "))
Will print:
{
"outter": {
"inner": {
"value": 10,
"value2": 20
},
"inner2": {
"value3": 30
}
}
}
Generating Arrays
jsonObj := gabs.New()
jsonObj.Array("foo", "array")
// Or .ArrayP("foo.array")
jsonObj.ArrayAppend(10, "foo", "array")
jsonObj.ArrayAppend(20, "foo", "array")
jsonObj.ArrayAppend(30, "foo", "array")
fmt.Println(jsonObj.String())
Will print:
{"foo":{"array":[10,20,30]}}
Working with arrays by index:
jsonObj := gabs.New()
// Create an array with the length of 3
jsonObj.ArrayOfSize(3, "foo")
jsonObj.S("foo").SetIndex("test1", 0)
jsonObj.S("foo").SetIndex("test2", 1)
// Create an embedded array with the length of 3
jsonObj.S("foo").ArrayOfSizeI(3, 2)
jsonObj.S("foo").Index(2).SetIndex(1, 0)
jsonObj.S("foo").Index(2).SetIndex(2, 1)
jsonObj.S("foo").Index(2).SetIndex(3, 2)
fmt.Println(jsonObj.String())
Will print:
{"foo":["test1","test2",[1,2,3]]}
Converting back to JSON
This is the easiest part:
jsonParsedObj, _ := gabs.ParseJSON([]byte(`{
"outter":{
"values":{
"first":10,
"second":11
}
},
"outter2":"hello world"
}`))
jsonOutput := jsonParsedObj.String()
// Becomes `{"outter":{"values":{"first":10,"second":11}},"outter2":"hello world"}`
And to serialize a specific segment is as simple as:
jsonParsedObj := gabs.ParseJSON([]byte(`{
"outter":{
"values":{
"first":10,
"second":11
}
},
"outter2":"hello world"
}`))
jsonOutput := jsonParsedObj.Search("outter").String()
// Becomes `{"values":{"first":10,"second":11}}`
Merge two containers
You can merge a JSON structure into an existing one, where collisions will be converted into a JSON array.
jsonParsed1, _ := ParseJSON([]byte(`{"outter": {"value1": "one"}}`))
jsonParsed2, _ := ParseJSON([]byte(`{"outter": {"inner": {"value3": "three"}}, "outter2": {"value2": "two"}}`))
jsonParsed1.Merge(jsonParsed2)
// Becomes `{"outter":{"inner":{"value3":"three"},"value1":"one"},"outter2":{"value2":"two"}}`
Arrays are merged:
jsonParsed1, _ := ParseJSON([]byte(`{"array": ["one"]}`))
jsonParsed2, _ := ParseJSON([]byte(`{"array": ["two"]}`))
jsonParsed1.Merge(jsonParsed2)
// Becomes `{"array":["one", "two"]}`
Parsing Numbers
Gabs uses the json
package under the bonnet, which by default will parse all
number values into float64
. If you need to parse Int
values then you should
use a json.Decoder
(https://golang.org/pkg/encoding/json/#Decoder):
sample := []byte(`{"test":{"int":10, "float":6.66}}`)
dec := json.NewDecoder(bytes.NewReader(sample))
dec.UseNumber()
val, err := gabs.ParseJSONDecoder(dec)
if err != nil {
t.Errorf("Failed to parse: %v", err)
return
}
intValue, err := val.Path("test.int").Data().(json.Number).Int64()
Documentation
¶
Overview ¶
Package gabs implements a simplified wrapper around creating and parsing unknown or dynamic JSON.
Index ¶
- Variables
- type Container
- func (g *Container) Array(path ...string) (*Container, error)
- func (g *Container) ArrayAppend(value interface{}, path ...string) error
- func (g *Container) ArrayAppendP(value interface{}, path string) error
- func (g *Container) ArrayCount(path ...string) (int, error)
- func (g *Container) ArrayCountP(path string) (int, error)
- func (g *Container) ArrayElement(index int, path ...string) (*Container, error)
- func (g *Container) ArrayElementP(index int, path string) (*Container, error)
- func (g *Container) ArrayI(index int) (*Container, error)
- func (g *Container) ArrayOfSize(size int, path ...string) (*Container, error)
- func (g *Container) ArrayOfSizeI(size, index int) (*Container, error)
- func (g *Container) ArrayOfSizeP(size int, path string) (*Container, error)
- func (g *Container) ArrayP(path string) (*Container, error)
- func (g *Container) ArrayRemove(index int, path ...string) error
- func (g *Container) ArrayRemoveP(index int, path string) error
- func (g *Container) Bytes() []byte
- func (g *Container) BytesIndent(prefix string, indent string) []byte
- func (g *Container) Children() ([]*Container, error)
- func (g *Container) ChildrenMap() (map[string]*Container, error)
- func (g *Container) Data() interface{}
- func (g *Container) Delete(path ...string) error
- func (g *Container) DeleteP(path string) error
- func (g *Container) EncodeJSON(encodeOpts ...EncodeOpt) []byte
- func (g *Container) Exists(hierarchy ...string) bool
- func (g *Container) ExistsP(path string) bool
- func (g *Container) Index(index int) *Container
- func (g *Container) JSONPointer(path string) (*Container, error)
- func (g *Container) Merge(source *Container) error
- func (g *Container) MergeFn(source *Container, ...) error
- func (g *Container) Object(path ...string) (*Container, error)
- func (g *Container) ObjectI(index int) (*Container, error)
- func (g *Container) ObjectP(path string) (*Container, error)
- func (g *Container) Path(path string) *Container
- func (g *Container) S(hierarchy ...string) *Container
- func (g *Container) Search(hierarchy ...string) *Container
- func (g *Container) Set(value interface{}, path ...string) (*Container, error)
- func (g *Container) SetIndex(value interface{}, index int) (*Container, error)
- func (g *Container) SetJSONPointer(value interface{}, path string) error
- func (g *Container) SetP(value interface{}, path string) (*Container, error)
- func (g *Container) String() string
- func (g *Container) StringIndent(prefix string, indent string) string
- type EncodeOpt
Constants ¶
Variables ¶
var ( // ErrOutOfBounds indicates an index was out of bounds. ErrOutOfBounds = errors.New("out of bounds") // ErrNotObjOrArray is returned when a target is not an object or array type // but needs to be for the intended operation. ErrNotObjOrArray = errors.New("not an object or array") // ErrNotObj is returned when a target is not an object but needs to be for // the intended operation. ErrNotObj = errors.New("not an object") // ErrNotArray is returned when a target is not an array but needs to be for // the intended operation. ErrNotArray = errors.New("not an array") // ErrPathCollision is returned when creating a path failed because an // element collided with an existing value. ErrPathCollision = errors.New("encountered value collision whilst building path") // ErrInvalidInputObj is returned when the input value was not a // map[string]interface{}. ErrInvalidInputObj = errors.New("invalid input object") // ErrInvalidInputText is returned when the input data could not be parsed. ErrInvalidInputText = errors.New("input text could not be parsed") // ErrInvalidPath is returned when the filepath was not valid. ErrInvalidPath = errors.New("invalid file path") // ErrInvalidBuffer is returned when the input buffer contained an invalid // JSON string. ErrInvalidBuffer = errors.New("input buffer contained invalid JSON") )
Functions ¶
Types ¶
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container references a specific element within a JSON structure.
func Consume ¶
Consume an already unmarshalled JSON object (or a new map[string]interface{}) into a *Container.
func ParseJSONBuffer ¶
ParseJSONBuffer reads a buffer and unmarshals the contents into a *Container.
func ParseJSONDecoder ¶
ParseJSONDecoder applies a json.Decoder to a *Container.
func ParseJSONFile ¶
ParseJSONFile reads a file and unmarshals the contents into a *Container.
func (*Container) Array ¶
Array creates a new JSON array at a path. Returns an error if the path contains a collision with a non object type.
func (*Container) ArrayAppend ¶
ArrayAppend attempts to append a value onto a JSON array at a path. If the target is not a JSON array then it will be converted into one, with its original contents set to the first element of the array.
func (*Container) ArrayAppendP ¶
ArrayAppendP attempts to append a value onto a JSON array at a path using dot notation. If the target is not a JSON array then it will be converted into one, with its original contents set to the first element of the array.
func (*Container) ArrayCount ¶
ArrayCount counts the number of elements in a JSON array at a path.
func (*Container) ArrayCountP ¶
ArrayCountP counts the number of elements in a JSON array at a path using dot notation.
func (*Container) ArrayElement ¶
ArrayElement attempts to access an element by an index from a JSON array at a path.
func (*Container) ArrayElementP ¶
ArrayElementP attempts to access an element by an index from a JSON array at a path using dot notation.
func (*Container) ArrayI ¶
ArrayI creates a new JSON array within an array at an index. Returns an error if the element is not an array or the index is out of bounds.
func (*Container) ArrayOfSize ¶
ArrayOfSize creates a new JSON array of a particular size at a path. Returns an error if the path contains a collision with a non object type.
func (*Container) ArrayOfSizeI ¶
ArrayOfSizeI create a new JSON array of a particular size within an array at an index. Returns an error if the element is not an array or the index is out of bounds.
func (*Container) ArrayOfSizeP ¶
ArrayOfSizeP creates a new JSON array of a particular size at a path using dot notation. Returns an error if the path contains a collision with a non object type.
func (*Container) ArrayP ¶
ArrayP creates a new JSON array at a path using dot notation. Returns an error if the path contains a collision with a non object type.
func (*Container) ArrayRemove ¶
ArrayRemove attempts to remove an element identified by an index from a JSON array at a path.
func (*Container) ArrayRemoveP ¶
ArrayRemoveP attempts to remove an element identified by an index from a JSON array at a path using dot notation.
func (*Container) BytesIndent ¶
BytesIndent marshals an element to a JSON []byte blob formatted with a prefix and indent string.
func (*Container) Children ¶
Children returns a slice of all children of an array element. This also works for objects, however, the children returned for an object will be in a random order and you lose the names of the returned objects this way.
func (*Container) ChildrenMap ¶
ChildrenMap returns a map of all the children of an object element.
func (*Container) Data ¶
func (g *Container) Data() interface{}
Data returns the underlying interface{} of the target element in the JSON structure.
func (*Container) Delete ¶
Delete an element at a path, an error is returned if the element does not exist.
func (*Container) DeleteP ¶
DeleteP deletes an element at a path using dot notation, an error is returned if the element does not exist.
func (*Container) EncodeJSON ¶
EncodeJSON marshals an element to a JSON formatted []byte using a variant list of modifier functions for the encoder being used. Functions for modifying the output are prefixed with EncodeOpt, e.g. EncodeOptHTMLEscape.
func (*Container) Index ¶
Index attempts to find and return an element within a JSON array by an index.
func (*Container) JSONPointer ¶
JSONPointer parses a JSON pointer path (https://tools.ietf.org/html/rfc6901) and either returns a *gabs.Container containing the result or an error if the referenced item could not be found.
func (*Container) Merge ¶
Merge a source object into an existing destination object. When a collision is found within the merged structures (both a source and destination object contain the same non-object keys) the result will be an array containing both values, where values that are already arrays will be expanded into the resulting array.
It is possible to merge structures will different collision behaviours with MergeFn.
func (*Container) MergeFn ¶
func (g *Container) MergeFn(source *Container, collisionFn func(destination, source interface{}) interface{}) error
MergeFn merges two objects using a provided function to resolve collisions.
The collision function receives two interface{} arguments, destination (the original object) and source (the object being merged into the destination). Which ever value is returned becomes the new value in the destination object at the location of the collision.
func (*Container) Object ¶
Object creates a new JSON object at a target path. Returns an error if the path contains a collision with a non object type.
func (*Container) ObjectI ¶
ObjectI creates a new JSON object at an array index. Returns an error if the object is not an array or the index is out of bounds.
func (*Container) ObjectP ¶
ObjectP creates a new JSON object at a target path using dot notation. Returns an error if the path contains a collision with a non object type.
func (*Container) Search ¶
Search attempts to find and return an object within the JSON structure by following a provided hierarchy of field names to locate the target. If the search encounters an array and has not reached the end target then it will iterate each object of the array for the target and return all of the results in a JSON array.
func (*Container) Set ¶
Set the value of a field at a JSON path, any parts of the path that do not exist will be constructed, and if a collision occurs with a non object type whilst iterating the path an error is returned.
func (*Container) SetIndex ¶
SetIndex attempts to set a value of an array element based on an index.
func (*Container) SetJSONPointer ¶
SetJSONPointer parses a JSON pointer path (https://tools.ietf.org/html/rfc6901) and sets the leaf to a value. Returns an error if the pointer could not be resolved due to missing fields.
func (*Container) SetP ¶
SetP sets the value of a field at a JSON path using dot notation, any parts of the path that do not exist will be constructed, and if a collision occurs with a non object type whilst iterating the path an error is returned.
type EncodeOpt ¶
EncodeOpt is a functional option for the EncodeJSON method.
func EncodeOptHTMLEscape ¶
EncodeOptHTMLEscape sets the encoder to escape the JSON for html.
func EncodeOptIndent ¶
EncodeOptIndent sets the encoder to indent the JSON output.