# Count elements in JSON payload

**URL:** https://discuss.elastic.co/t/count-elements-in-json-payload/268325
**Category:** Kibana
**Created:** [March 25, 2021, 11:15am UTC](https://discuss.elastic.co/t/count-elements-in-json-payload/268325 "2021-03-25T11:15:13Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![cjeanne](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@cjeanne](https://discuss.elastic.co/u/cjeanne)
#### Post date: [March 25, 2021, 11:15am UTC](https://discuss.elastic.co/t/count-elements-in-json-payload/268325/1 "2021-03-25T11:15:13Z")

</div>

Hello,  
I'm trying to detect index on read-only with a watcher. I use http input, and I juste want to count the result.

My json watcher :

```auto
{
  "trigger": {
    "schedule": {
      "interval": "10m"
    }
  },
  "input": {
    "http": {
      "request": {
        "scheme": "https",
        "host": " *****",
        "port": 9200,
        "method": "get",
        "path": "/*/_settings",
        "params": {
          "filter_path": "*.settings.index.blocks.read_only_allow_delete"
        },
        "headers": {},
        "auth": {
          "basic": {
            "username": " *****",
            "password": " *****"
          }
        }
      }
    }
  },
  "condition": {
    "compare": {
      "ctx.payload.hits.total": {
        "gte": 1
      }
    }
  },
  "actions": {
    "my-logging-action": {
      "logging": {
        "level": "warn",
        "text": "There are {{ctx.payload.hits.total}} read/only index"
      }
    }
  }
}

```

But in the result, the ctx.payload.hits.total is null (but i see correctly the index on read-only in the payload).

```auto
{
  "watch_id": "_inlined_",
  "node": "DzB7VTgaTGKh0oe8Lxt_oQ",
  "state": "execution_not_needed",
  "user": " *****",
  "status": {
    "state": {
      "active": true,
      "timestamp": "2021-03-25T11:13:06.071Z"
    },
    "last_checked": "2021-03-25T11:13:06.071Z",
    "actions": {
      "my-logging-action": {
        "ack": {
          "timestamp": "2021-03-25T11:13:06.071Z",
          "state": "awaits_successful_execution"
        }
      }
    },
    "execution_state": "execution_not_needed",
    "version": -1
  },
  "trigger_event": {
    "type": "manual",
    "triggered_time": "2021-03-25T11:13:06.071Z",
    "manual": {
      "schedule": {
        "scheduled_time": "2021-03-25T11:13:06.071Z"
      }
    }
  },
  "input": {
    "http": {
      "request": {
        "scheme": "https",
        "host": " *****",
        "port": 9200,
        "method": "get",
        "path": "/*/_settings",
        "params": {
          "filter_path": "*.settings.index.blocks.read_only_allow_delete"
        },
        "headers": {},
        "auth": {
          "basic": {
            "username": " *****",
            "password": "::es_redacted::"
          }
        }
      }
    }
  },
  "condition": {
    "compare": {
      "ctx.payload.hits.total": {
        "gte": 1
      }
    }
  },
  "metadata": {
    "name": "check-index-settings",
    "xpack": {
      "type": "json"
    }
  },
  "result": {
    "execution_time": "2021-03-25T11:13:06.071Z",
    "execution_duration": 29,
    "input": {
      "type": "http",
      "status": "success",
      "payload": {
        "_headers": {
          "content-length": [
            "103"
          ],
          "content-type": [
            "application/json; charset=UTF-8"
          ]
        },
        "_status_code": 200,
        " ***** _i-000071": {
          "settings": {
            "index": {
              "blocks": {
                "read_only_allow_delete": "true"
              }
            }
          }
        }
      },
      "http": {
        "request": {
          "host": " *****",
          "port": 9200,
          "scheme": "https",
          "method": "get",
          "path": "/*/_settings",
          "params": {
            "filter_path": "*.settings.index.blocks.read_only_allow_delete"
          },
          "auth": {
            "basic": {
              "username": " *****",
              "password": "::es_redacted::"
            }
          }
        },
        "status_code": 200
      }
    },
    "condition": {
      "type": "compare",
      "status": "success",
      "met": false,
      "compare": {
        "resolved_values": {
          "ctx.payload.hits.total": null
        }
      }
    },
    "actions": []
  },
  "messages": []
}

```

Do you have any idea?

---

<div class="post-metadata">

### Author: ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)
#### Post date: [March 26, 2021, 11:38pm UTC](https://discuss.elastic.co/t/count-elements-in-json-payload/268325/2 "2021-03-26T23:38:26Z")

</div>

Your HTTP input calls an Elasticsearch API:

> [@cjeanne](#):
>
> ```auto
> "scheme": "https",
> "host": " *****",
> "port": 9200,
> "method": "get",
> "path": "/*/_settings",
> "params": {
> "filter_path": "*.settings.index.blocks.read_only_allow_delete"
> },
> 
> ```

But your `compare` declaration is for parsing search results:

```auto
  "condition": {
    "compare": {
      "ctx.payload.hits.total": {
        "gte": 1
      }
    }

```

But your payload is an API response, not search results. You need to update the `compare` to something that will look for the results to be a non-empty object.

---

<div class="post-metadata">

### Author: ![cjeanne](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@cjeanne](https://discuss.elastic.co/u/cjeanne)
#### Post date: [March 27, 2021, 5:30pm UTC](https://discuss.elastic.co/t/count-elements-in-json-payload/268325/3 "2021-03-27T17:30:18Z")

</div>

Thank you @tsullivan for your support.  
I suppose I need to use "script" condition?  
But I didn't find the way to use "ctx.payload".  
The goal is not only to know if I have index on readonly, or not (empty response), but also to count how many index in this status.  
Can you guide me please?

---

<div class="post-metadata">

### Author: ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)
#### Post date: [March 30, 2021, 5:26pm UTC](https://discuss.elastic.co/t/count-elements-in-json-payload/268325/4 "2021-03-30T17:26:42Z")

</div>

I think you just need to know the number of indices in the response, which I believe is a HashMap. Something like this might work:

```auto
  "condition": {
    "script": {
      "source": "return ctx.size()",
      "lang": "painless"
    }
  },

```

If you need further help, you'll have to take some time to learn Painless. Here is the documentation on HashMap: [Shared API for package java.util | Painless Scripting Language [7.12] | Elastic](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-api-reference-shared-java-util.html#painless-api-reference-shared-HashMap)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [April 27, 2021, 5:27pm UTC](https://discuss.elastic.co/t/count-elements-in-json-payload/268325/5 "2021-04-27T17:27:30Z")

</div>

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