SoFunction
Updated on 2025-04-13

Go dynamically modify JSON through SJSON

In the field of Go language json processing, in json data processing, reading and modification are two core requirements. The previous article introducedGJSONSolved the flexible reading problem, andSJSONAs its sister library, it focuses on implementing dynamic json modifications without struct definition.

This article will continue the comparative analysis style and analyze the core values ​​of SJSON.

1. Go native json modification method

To modify json data natively, you also need to define the structure first, and then parse the json data to the structure instance, such as:

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
    }

     = 35
    newJson, _ := (person)

    (string(newJson))
}

2. SJSON components

1. Overview

SJSON provides the ability to directly modify json strings through path expressions, and uses the same path syntax as GJSON to form a closed loop of read and write.

Official website address:GitHub - tidwall/sjson

2. Installation

Use Go's package management toolgo getInstall SJSON:

go get -u /tidwall/sjson

3. SJSON core usage

1. Modify the basic value

package main

import (
    "fmt"
    "/tidwall/sjson"
)

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

    // Modify the age value to 35    newJson, _ := (jsonStr, "age", 35)

    (string(newJson))
}

2. Nested structure modification

package main

import (
    "fmt"
    "/tidwall/sjson"
)

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

    // Modify the value: Just because you are too beautiful => Rebirth    newJson, _ := (jsonStr, "", "Rebirth")

    (string(newJson))
}

3. Array operation

package main

import (
    "fmt"
    "/tidwall/sjson"
)

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

    // Modify the fourth element of the hobby array to play    newJson, _ := (jsonStr, "hobby.3", "play")

    (string(newJson))

    // Append the fifth element of the hobby array to play    newJson, _ = (jsonStr, "tags.-1", "play")
    (string(newJson))
}

4. Field Delete

package main

import (
    "fmt"
    "/tidwall/sjson"
)

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

    // Delete the age field    newJson, _ := (jsonStr, "age")

    (string(newJson))
}

4. Comparison of SJSON and native solutions

  • SJSON breaks away from the constraints of structure definitions, maintains the integrity of the original json structure, and avoids the problem of losing undefined fields after modification.
  • The SJSON path goes directly to the modification location, avoiding the problems caused by nesting nested structures, and forming a complete processing link with GJSON.
  • SJSON supports runtime dynamic path construction to avoid problems caused by hard-coded paths.

This is the article about Go dynamically modifying JSON through SJSON. For more relevant Go dynamically modifying JSON content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!