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




Here is a simple solution to give you an idea.

1 Like

@ermanimer’s solution is good and complete but i think if your response json’s key is dynamic the solution is like this:
i just change some line of @ermanimer’s solution


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[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 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

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)

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

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

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.