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

How to get next value under ForEach function #342

Open
flenoir opened this issue Dec 11, 2023 · 2 comments
Open

How to get next value under ForEach function #342

flenoir opened this issue Dec 11, 2023 · 2 comments

Comments

@flenoir
Copy link

flenoir commented Dec 11, 2023

Hi,

I did check all repo and couldn't find an answer about this.

Mu code is like below and i'm using "ForEach" to loop on a json object. I'd like to compare the current value to the next one to check if the values are the same.


	result2 := gjson.Get(json2, "hits.hits")

	result2.ForEach(func(key, value gjson.Result) bool {
		leresultatTmj := newEntiteTmj(value.Get("_source.kubernetes.labels.mycode").String(), value.Get("_source.kubernetes.myteam").String())
		println("current results are : ", leresultatTmj.composantTmj, leresultatTmj.team)
		return true // keep iterating
	})

How can i access the next value ? i did try to use "key" valueassociated to "value" but couldn't find a proper way.

Is it possible to do so ?

Thanks

@alberto-bogoni
Copy link

Hi! You can't treat directly a json object like an ordered list of key/value pairs

@tidwall
Copy link
Owner

tidwall commented Feb 25, 2024

I don't know if this helps but you can order the keys using @pretty:{"sortKeys":true}

For example:

result2 := gjson.Get(json2, "@pretty:{"sortKeys":true}|hits.hits")

This will ensure that all keys for the entire JSON are sorted.

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

3 participants