SoFunction
Updated on 2025-04-13

Go uses GJSON component to unlock JSON reading new poses

In the field of Go language development, json data processing is an extremely common task. The Go standard library providesencoding/jsonPackage is used to process json data, and at the same time, third-party librariesGJSON & SJSONAlso doing well in json processing.

This article will explore the GJSON component in depth. By comparing it with native processing methods, what is special about it and what its advantages are reflected in.

1. Go native json reading method

Go natively reads json data. Usually, you need to define the structure first, and then parse the json data to the structure instance, such as:

{
    "name": "Zhang San",
    "age": 25
}

Specific processing logic:

package main

import (
    "encoding/json"
    "fmt"
)

type Person struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

func main() {
    jsonStr := `{"name":"Zhang San","age":25}`

    var person Person
    err := ([]byte(jsonStr), &person)
    if err!= nil {
        ("Parse error:", err)
        return
    }

    ("Name:", )
    ("Age:", )
}

Although this method can accurately parse json data, if json has multiple nesting and over-packaging at levels, the structure definition and parsing process will become quite cumbersome.

2. GJSON components

1. Overview

GJSON is a lightweight and high-performance JSON parsing library that allows developers to quickly extract specific values ​​in JSON data through concise syntax without defining structures.

Official website address:GitHub - tidwall/gjson

2. Installation

Use Go's package management toolgo getInstall GJSON:

go get -u /tidwall/gjson

3. Basic usage of GJSON

1. Simple json data acquisition

For simple json, like the previous example, use it directlyMethod, pass in the json string and the field name to be obtained, and you can get the corresponding value. For example, getnameFields,(jsonStr, "name")It can be done, for example:

package main

import (
    "fmt"
    "/tidwall/gjson"
)

func main() {
    jsonStr := `{"name":"Zhang San","age":25}`

    name := (jsonStr, "name")
    age := (jsonStr, "age")

    ("Name:", ())
    ("Age:", ())
}

2. Nested json data acquisition

As mentioned above, the native processing method is very unfriendly to multi-level jsons, but gjon can directly separate the path of the path through the dots, and its advantages gradually become obvious, for example:

{
    "name": "Zhang San",
    "age": 25,
    "hobby": {
        "sing": "Just because you are too beautiful",
        "dance": "Overall",
        "rap": "kun",
        "ball": "basketball"
    }
}

Specific processing logic:

package main

import (
    "fmt"
    "/tidwall/gjson"
)

func main() {
    jsonStr := `{
       "name": "Zhang San",
       "age": 25,
       "hobby": {
          "sing": "Just because you are too beautiful",
          "dance": "Overall",
          "rap": "kun",
          "ball": "basketball"
       }`

    name := (jsonStr, "name")
    ball := (jsonStr, "")

    ("Name:", ())
    ("ball:", ())
}

Compared with the native method, it does not require complex structure definitions and is easier to operate.

3. json array processing

If an array is nested in json, it is relatively simple to handle this kind of processing. You can directly locate the data through the array subscript, such as:

package main

import (
    "fmt"
    "/tidwall/gjson"
)

func main() {
    jsonStr := `{"hobby": ["sing","dance","rap","ball"]}`

    hobby := (jsonStr, "hobby.3")
  
    // Output the 4th hobby    ("hobby:", ())
}

Compared to the native way of processing arrays, you have to parse them into slices first, which is not so direct in operation.

4. Comparison of GJSON and native JSON processing methods

  • GJSON syntax is simple and intuitive. You can get started quickly by being familiar with json structure without learning about structure definition and label usage. The native method is relatively complex in structure definition, especially when dealing with complex json structures.
  • GJSON does not need to parse the entire json data into a structure. When processing large json data, it consumes less memory and has a fast parsing speed. When parsing complex json data in native mode, structure construction and memory allocation overhead is high.
  • GJSON can flexibly respond to various complex json structures, obtain data according to the path according to needs, without frequent modification of the code structure. The native method needs to change the structure definition according to the json structure, and the structure definition is frequently modified, which has poor flexibility.

This is the article about Go using GJSON components to unlock new JSON reading postures. For more related Go GJSON reading content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!