Filter json values to some conditional return

I bit confuse to filter values with some conditional returns, I have this response, that I parsed from grafana request. How can I bring out only if values > 25?

Thank you In advanced

{
"ha-external-01":45.804,
"twem-01":20.800243,
"redis-01":20.8902,
"redis-03":20.89015,
"arrango-01":20.8890865
}

expected

{"ha-external-01":45.804}

Here is a simple solution to give you an idea.

https://play.golang.org/p/5uP1Xuf1rqm

1 Like

Hi
@ermanimer’s solution is good and complete but i think if your response json’s key is dynamic the solution is like this:
https://play.golang.org/p/l8KNgQhByo0
i just change some line of @ermanimer’s solution

2 Likes

Than you :slight_smile:

Hi @shirejoni, apologies for the late reply. How can I return values without map[] map[] notation eg

{"ha-external-01":45.804, "redis-b-03":35.3342, "kafka-a-01":47.54754 }

Eg my return values:

map[]
map[redis-b-03:0.323432]
map[docker-a-01:0.652938221 neo-redis-b-03:0.300601]
map[kafka-a-01:0.4754754 docker-a-01:0.6529382 redis-b-03:0.30060120]

Not sure what you are asking, but map[…] is just a debugging representation. It is not “returned”, neither do you need to return values “without it”.

Hi @NobbZ,

I mean how to get one single return values like this not below:

[ha-external-a-01:59.48262787184605 php-a-01:68.34888750379501 mysql-b-02:61.23623409094686]
map[ha-external-a-01:59.48262787184605]
map[ha-external-a-01:59.48262787184605 php-a-01:68.34888750379501]
map[ha-external-a-01:59.48262787184605 php-a-01:68.34888750379501 mysql-b-02:61.23623409094686]

My snippet code based on @ermanimer

	filtered = make(map[string]float64)
	for key, value := range response {
		if value > 55 {
			filtered[key] = value
			fmt.Println(filtered)
		}
	}

You are not returning anything, just printing out stuff. You do this each time your filter condition was true.

You can return values using the return statement.

Though its not clear to me whether you want to return the map after filtering the input map, or just the first encountered value.

Apologies for the basic question :confused:, I mean how to get a list of values like this :

[ha-external-a-01:59.48262787184605 php-a-01:68.34888750379501 mysql-b-02:61.23623409094686]

I have figured out using append, but it only append 1 by 1 values:

	filtered = make(map[string]float64)
	var out []string
	for key, value := range response {
		if value > 55 {
			filtered[key] = value
			// fmt.Println(key, value)
			out = append(out, key)
			fmt.Println(out)
		}
	}
}

https://play.golang.org/p/LG-lXr0cenV

1 Like

Thanks for your reply @clbanning, my purpose is sending filtered values using SMTP Gmail. I got some errors like this while calling send func() inside message []byte :

	message := []byte("Subject:" + subject + "\n\n" +
		"CPU with the condition over than 80%: " +
		fmt.Sprintf(`{"items":"%v"}`, checklink))

My error:

checkURLS(links) used as value
too many arguments to return
        have (string)
        want ()

any suggestion? thanks for your help. My plain full code on this https://play.golang.org/p/po43WuzhGzC

https://play.golang.org/p/p_X-ABNkTae

You’ve got typos, don’t use limit, and should handle errors. In the end, the message is fine.