gjson icon indicating copy to clipboard operation
gjson copied to clipboard

Is it possible to prune elements?

Open jdblack opened this issue 4 years ago • 1 comments

Thank you for gjson! I think it's going to really help me out on my first go project! As you can undoubtedly surmise, I'm rather new to the language, so I apologize in advance if I don't get this quite right.

I'm trying to figure out the right way to use gjson to prune particular elements from my json structure. I'm imagining a prototype along the lines of:

func (t Result) Prune(path string) Result

As imagined by : https://play.golang.org/p/MRhWvOaVHBs It seems, though, that gjson doesn't have this ability. I see some references in the code to squashing json in the code, but they seem to be unintended for my usage.

Should I be taking an approach outside of gjson, perhaps unmarshaling the json myself, pruning the maps, then remarshal it so that I can then use gjson do the tree walking that I would like?

Thanks again!

jdblack avatar May 13 '20 07:05 jdblack

I recommend you take a look at github.com/tidwall/sjson if you are looking to modify JSON values. In the particular example you provided, I believe json, _ = sjson.Delete(json, "age") will do what you expect.

cjkreklow avatar May 15 '20 02:05 cjkreklow