Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible bug with template and custom modifier #253

Closed
natenho opened this issue Nov 25, 2021 · 2 comments
Closed

Possible bug with template and custom modifier #253

natenho opened this issue Nov 25, 2021 · 2 comments

Comments

@natenho
Copy link

natenho commented Nov 25, 2021

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
Copy link
Owner

tidwall commented Nov 25, 2021

I just pushed a fix. Thanks for reporting.

@natenho
Copy link
Author

natenho commented Nov 25, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants