SoFunction
Updated on 2025-03-05

Detailed explanation of how to use the Go language JSON parser gjson

gjson

GJSONIt's oneGopackage, it provides ajsonA quick and easy way to get values ​​in a document. It has functions such as single-line retrieval, point symbol path, iteration and parsing json lines.

You can also viewSJSONTo modify json,as well asJJCommand line tool.

How to use this readme fileGJSONA quick overview, for more information, please check outGJSONgrammar.

The address of github ishere

Install

Installgjson, usinggoTraditional installation method:

go install /tidwall/gjson@latest

Execute in the file directory:

$ go get -u /tidwall/gjson

This is in the directory,gjsonNow.

use

Get the corresponding value.

Get the search for the specified pathjson. The path adopts point syntax, such as "" or "age". When the value is found, it returns immediately.

package main
import "/tidwall/gjson"
const json = `{"name":{"first":"Janet","last":"Prichard"},"age":47}`
func main() {
	value := (json, "")
	println(())
}

This will print:

Prichard

This is the traditional one we usejsonThere is a little difference in analysis,gjsonNot only did we analyze itjsonThe data also helps us establish a channel for quick search.

Path syntax

A path is a series of keys separated by points. The key may contain special wildcards "*" and "?". To access array values, use the index as the key. To get the number of elements in an array or access subpaths, use the "#" character. Dots and wildcards can be escaped with "\".

{
  "name": {"first": "Tom", "last": "Anderson"},
  "age":37,
  "children": ["Sara","Alex","Jack"],
  "": "Deer Hunter",
  "friends": [
    {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
    {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
    {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
  ]
}

""          >> "Anderson"
"age"                >> 37
"children"           >> ["Sara","Alex","Jack"]
"children.#"         >> 3
"children.1"         >> "Alex"
"child*.2"           >> "Jack"
"c?ildren.0"         >> "Sara"
"fav\.movie"         >> "Deer Hunter"
"friends.#.first"    >> ["Dale","Roger","Jane"]
"friends."     >> "Craig"

You can also use queries the first match in the array#(…) or find all matches#(…)#. Query supports the ==, !=, <, <=, >,>= comparison operators with simple pattern matching %(like) and !% (not like) operators.

friends.#(last=="Murphy").first    >> "Dale"
friends.#(last=="Murphy")#.first   >> ["Dale","Jane"]
friends.#(age>45)#.last            >> ["Craig","Murphy"]
friends.#(first%"D*").last         >> "Murphy"
friends.#(first!%"D*").last        >> "Craig"
friends.#(nets.#(=="fb"))#.first   >> ["Dale","Roger"]

This makes it very convenient for us to search.

This is the end of this article about the detailed explanation of how to use the Go language JSON parser gjson. For more related contents of Go JSON parser gjson, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!