文档:https://pkg.go.dev/github.com/tidwall/gjson

Func

  1. func AddModifier(name string, fn func(json, arg string) string)
  2. func ForEachLine(json string, iterator func(line Result) bool)
  3. func Valid(json string) bool
  4. func ValidBytes(json []byte) bool
  5. func Get(json, path string) Result
  6. func GetBytes(json []byte, path string) Result
  7. func GetMany(json string, path ...string) []Result
  8. func GetManyBytes(json []byte, path ...string) []Result
  9. func Parse(json string) Result
  10. func ParseBytes(json []byte) Result

type Result

  1. type Result struct {
  2. // Type is the json type
  3. Type Type
  4. // Raw is the raw json
  5. Raw string
  6. // Str is the json string
  7. Str string
  8. // Num is the json number
  9. Num float64
  10. // Index of raw value in original json, zero means index unknown
  11. Index int
  12. // Indexes of all the elements that match on a path containing the '#'
  13. // query character.
  14. Indexes []int
  15. }
  16. // Array返回一个值的数组。如果结果表示空值或不存在,则返回一个空数组。
  17. // 如果结果不是一个JSON数组,返回值将是一个包含一个结果的数组。
  18. func (t Result) Array() []Result
  19. func (t Result) Bool() bool
  20. func (t Result) Exists() bool
  21. func (t Result) Float() float64
  22. func (t Result) String() string
  23. func (t Result) Time() time.Time
  24. func (t Result) Uint() uint64=
  25. func (t Result) Int() int64
  26. func (t Result) IsArray() bool
  27. func (t Result) IsBool() bool
  28. func (t Result) IsObject() bool
  29. func (t Result) Map() map[string]Result
  30. func (t Result) Path(json string) string // Path返回Result的原始GJSON路径
  31. func (t Result) Value() interface{}
  32. // bool, for JSON booleans
  33. // float64, for JSON numbers
  34. // Number, for JSON numbers
  35. // string, for JSON string literals
  36. // nil, for JSON null
  37. // map[string]interface{}, for JSON objects
  38. // []interface{}, for JSON arrays

type Type

  1. type Type int
  2. const (
  3. // Null is a null json value
  4. Null Type = iota
  5. // False is a json false boolean
  6. False
  7. // Number is json number
  8. Number
  9. // String is a json string
  10. String
  11. // True is a json true boolean
  12. True
  13. // JSON is a raw block of JSON
  14. JSON
  15. )
  16. func (t Type) String() string

Get a value

  1. package main
  2. import "github.com/tidwall/gjson"
  3. const json = `{"name":{"first":"Janet","last":"Prichard"},"age":47}`
  4. func main() {
  5. value := gjson.Get(json, "name.last")
  6. println(value.String()) // Prichard
  7. }
  8. // 下面三种写法,是相同的结果
  9. gjson.Parse(json).Get("name").Get("last")
  10. gjson.Get(json, "name").Get("last")
  11. gjson.Get(json, "name.last")

Path Syntax

*: 通配符
?: 占位符
\: 转义符

  1. {
  2. "name": {"first": "Tom", "last": "Anderson"},
  3. "age":37,
  4. "children": ["Sara","Alex","Jack"],
  5. "fav.movie": "Deer Hunter",
  6. "friends": [
  7. {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
  8. {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
  9. {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
  10. ]
  11. }
  12. "name.last" >> "Anderson"
  13. "age" >> 37
  14. "children" >> ["Sara","Alex","Jack"]
  15. "children.#" >> 3
  16. "children.1" >> "Alex"
  17. "child*.2" >> "Jack"
  18. "c?ildren.0" >> "Sara"
  19. "fav\.movie" >> "Deer Hunter"
  20. "friends.#.first" >> ["Dale","Roger","Jane"]
  21. "friends.1.last" >> "Craig"

Arrays

  1. friends.# 3
  2. friends.#.age [44,68,47]

Queries

(…):匹配第一个数组
#(…)#:匹配所有数组
支持 == != < <=``> >= % (like) !% (not like)

  1. friends.#(last=="Murphy").first "Dale"
  2. friends.#(last=="Murphy")#.first ["Dale","Jane"]
  3. friends.#(age>45)#.last ["Craig","Murphy"]
  4. friends.#(first%"D*").last "Murphy"
  5. friends.#(first!%"D*").last "Craig"

使用非对象,查询元素不包含

  1. children.#(!%"*a*") "Alex"
  2. children.#(%"*a*")# ["Sara","Jack"]

Modifiers

编辑器,例如下面反转数组

  1. children.@reverse ["Jack","Alex","Sara"]
  2. children.@reverse.0 "Jack"
  • @reverse: Reverse an array or the members of an object.
  • @ugly: Remove all whitespace from JSON.
  • @pretty: Make the JSON more human readable.
  • @this: Returns the current element. It can be used to retrieve the root element.
  • @valid: Ensure the json document is valid.
  • @flatten: Flattens an array.
  • @join: Joins multiple objects into a single object.
  • @keys: Returns an array of keys for an object.
  • @values: Returns an array of values for an object.
  • @tostr: Converts json to a string. Wraps a json string.
  • @fromstr: Converts a string from json. Unwraps a json string.
  • @group: Groups arrays of objects