jsonT

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Enter

type Enter struct{}

func (Enter) FromJSON

func (js Enter) FromJSON(j string, o any) *any

func (Enter) GetValue

func (js Enter) GetValue(src string, path string) gjson.Result

GetValue searches json for the specified path. A path is in dot syntax, such as "name.last" or "age". When the value is found it's returned immediately.

A path is a series of keys separated by a dot. A key may contain special wildcard characters '*' and '?'. To access an array value use the index as the key. To get the number of elements in an array or to access a child path, use the '#' character. The dot and wildcard character can be escaped with '\'.

{
  "name": {"first": "Tom", "last": "Anderson"},
  "age":37,
  "children": ["Sara","Alex","Jack"],
  "friends": [
    {"first": "James", "last": "Murphy"},
    {"first": "Roger", "last": "Craig"}
  ]
}
"name.last"          >> "Anderson"
"age"                >> 37
"children"           >> ["Sara","Alex","Jack"]
"children.#"         >> 3
"children.1"         >> "Alex"
"child*.2"           >> "Jack"
"c?ildren.0"         >> "Sara"
"friends.#.first"    >> ["James","Roger"]

This function expects that the json is well-formed, and does not validate. Invalid json will not panic, but it may return back unexpected results. If you are consuming JSON from an unpredictable source then you may want to use the Valid function first.

func (Enter) GetValues

func (js Enter) GetValues(src string, path ...string) []gjson.Result

GetValues searches json for the multiple paths. The return value is a Result array where the number of items will be equal to the number of input paths.

func (Enter) ToJson

func (js Enter) ToJson(obj interface{}) string

ToJson converts obj to JSON.

func (Enter) ToObjB

func (js Enter) ToObjB(src []byte, obj interface{}) bool

ToObjB converts []byte to obj.

func (Enter) ToObjI

func (js Enter) ToObjI(src interface{}, obj interface{}) bool

ToObjI converts map or slice to JSON.

func (Enter) ToObjS

func (js Enter) ToObjS(src string, obj interface{}) bool

ToObjS converts JSON to obj.

func (Enter) Valid

func (js Enter) Valid(src string) bool

Valid checks if the JSON is valid.

Jump to

Keyboard shortcuts

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