tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
14.1k stars 846 forks source link

Possible bug with template and custom modifier #253

Closed natenho closed 2 years ago

natenho commented 2 years ago

Hello, I'm trying to apply a custom modifier to a field and the expected output should be the complete json, although it is ignoring the remaining fields.

Reproduction

package main

import (
    "fmt"
    "strings"

    "github.com/tidwall/gjson"
)

func main() {

    gjson.AddModifier("case", func(json, arg string) string {
        if arg == "upper" {
            return strings.ToUpper(json)
        }
        if arg == "lower" {
            return strings.ToLower(json)
        }
        return json
    })

    json := `
    {
        "name": {"first": "Tom", "last": "Anderson"},
        "age":37,
        "children": ["Sara","Alex","Jack"],
        "fav.movie": "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"]}
        ]
      }
    `
    result := gjson.Get(json, `{"children":children|@case:upper,"name":name.first,"age":age}`)
    fmt.Println(result.Raw)
}

Expected Output (all fields)

{"children":["SARA","ALEX","JACK"], "name":"Tom", "age": 37}

Actual Output (missing all the other fields)

{"children":["SARA","ALEX","JACK"]}
tidwall commented 2 years ago

I just pushed a fix. Thanks for reporting.

natenho commented 2 years ago

@tidwall It's working now! Thank you for the amazingly fast response !!🥇