Iterate over json root keys
Opened this issue · 2 comments
ejemba commented
Hi,
I don't see the way to iterate over keys at root level,
in this example there is one key at root level it is "object"
jsonParsed, err := gabs.ParseJSON([]byte(`{"object":{"first":1,"second":2,"third":3}}`))
if err != nil {
panic(err)
}
// S is shorthand for Search
for key, child := range jsonParsed.S("object").ChildrenMap() {
fmt.Printf("key: %v, value: %v\n", key, child.Data().(float64))
}
How can we iterate those keys ? I tried jsonParsed.S("")
it does not work
Thank you
mihaitodor commented
Hi @ejemba, thanks for reporting this issue! I'm not sure I follow what you're trying to achieve, though. If you're simply looking to traverse the root level keys, this code works:
jsonParsed, err := gabs.ParseJSON([]byte(`{"object":{"first":1,"second":2,"third":3}}`))
if err != nil {
panic(err)
}
for key, child := range jsonParsed.ChildrenMap() {
fmt.Printf("key: %v, value: %v\n", key, child.Data())
}
It will print key: object, value: map[first:1 second:2 third:3]
.
ejemba commented
Hi @mihaitodor , thank you for your solution :)
Was sure it was simple :)