Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterate over json root keys #109

Open
ejemba opened this issue Sep 17, 2021 · 2 comments
Open

Iterate over json root keys #109

ejemba opened this issue Sep 17, 2021 · 2 comments

Comments

@ejemba
Copy link

ejemba commented Sep 17, 2021

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
Copy link
Contributor

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
Copy link
Author

ejemba commented Oct 5, 2021

Hi @mihaitodor , thank you for your solution :)

Was sure it was simple :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants