gjson icon indicating copy to clipboard operation
gjson copied to clipboard

Question on joining as one string

Open isyca opened this issue 4 weeks ago • 1 comments

Sorry. i cannot find any discussion / discord.

	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"]}
		]
	}`

	value := gjson.Get(json, "name.first|@join|age")
	println(value.String()) 	// this prints nothing. I am looking to print it "Tom37" just like below
	
	val1 := gjson.Get(json, "name.first")
	val2 := gjson.Get(json, "age")
	println(val1.String()+ val2.String())

I am looking for a single path that will result to (in this example) "Tom37"

Thanks.

isyca avatar Nov 25 '25 07:11 isyca

@isyca not that I know of, this is similar to #373.

Also in this case you could just use multipaths to get a list of what you want to join (e.g. [name.first,age]) and then use a custom modifier to join the elements of an array into a string without spaces.

If the values you're interested are known to have a simple set of characters (e.g. just letters and numbers) another uglier and potentially error prone way could be to use the combination of a multipaths and the @tostr modifier and then remove the spurious characters from the output. But I would suggest to use the custom modifier approach. For example with a query [name.first,age].@tostr for example you get "[\"Tom\",37]".

volans- avatar Nov 25 '25 08:11 volans-