qntfy / kazaam

Arbitrary transformations of JSON in Golang
MIT License
282 stars 54 forks source link

Getting the root key #73

Closed xnaveira closed 6 years ago

xnaveira commented 6 years ago

If you have an object like this:

{ 
"myrootkey":
   "subkey1": "field1",
   "subkey2": "field2"
}

How do you get the "myrootkey" literal value?

ryanleary commented 6 years ago

I'm not sure we have a mechanism for that just yet. I could imagine creating a transform for mapping an object's keys to an array that might do what you want. We'd be happy to accept a PR for that kind of functionality.

JoshuaC215 commented 6 years ago

Hey @xnaveira you can do this with the underlying jsonparser library that kazaam is built on.

Basically call this with a callback function that prints the provided key: https://godoc.org/github.com/qntfy/jsonparser#ObjectEach