tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
13.95k stars 841 forks source link

Retrieve all possible keys? #304

Closed kpfaulkner closed 1 year ago

kpfaulkner commented 1 year ago

This looks really useful for a project I'm working on. But I'm just wondering if there is a way to easily get all possible "keys" from a given JSON?

Using the example JSON on the github page:

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

Is there a way to get the keys, similar to what is listed also on the github page:

"name.last"        
"age"            
"children"           
"children.#"        
"children.1"      
"fav\.movie"         
"friends.0.first"    
"friends.0.last"     

etc etc.

praveentiru commented 1 year ago

I do not believe there is a method to server that purpose those keys are actually queries a person would have to build to get the value/values he is looking for in the JSON document. If you want all keys, then you can unmarshal the json to a map and then get all the keys of the map.

kpfaulkner commented 1 year ago

Yeah, am going to roll my own... was hoping that there was something there already I could use. Cheers!