tidwall / sjson

Set JSON values very quickly in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DeleteBytes not working well for paths with escaped dot inside

cliu-splunk opened this issue · comments

I am able to read the data with gjson.GetBytes but have trouble to delete the data with sjson.DeleteBytes. See example below:

package main

import (
    "fmt"
    "encoding/json"

    "github.com/tidwall/gjson"
    "github.com/tidwall/sjson"
)

func main() {
    parameters := &map[string]interface{}{
	"data": map[string]interface{}{
	    "key1":     "value1",
	    "key2.something": "value2",
        },
    }

    path := "data.key2\\.something"

    data, err := json.Marshal(parameters)
    if err != nil {
        fmt.Println(err)
    }

    // Read
    res := gjson.GetBytes(data, path)
    if !res.Exists() {
        fmt.Println("res does not exist")
    }
    fmt.Println("res", res.Value())

    // Delete
    data, err = sjson.DeleteBytes(data, path)
    var params interface{}
    err = json.Unmarshal(data, &params)
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println("params", params)
}

I just pushed a fix. Thanks for reporting.

Thank you!