Using number variable inside query not working

  • What Grafana version and what operating system are you using?

Grafana v9.5.0 (4a2e069461)
Google Chrome
Mac osx

  • What are you trying to achieve?

I am trying to dynamically adjust the number of flags displayed in a table panel in Grafana based on a user-selected value from a template variable.

  • How are you trying to achieve it?

I have created a template variable named num_flags and configured it to provide a list of numbers from 1 to 50. I am using this variable in a JSON query within a table panel to set the size field dynamically based on the user’s selection.

  • What happened?

The table panel is not displaying the expected number of flags. The table seems to show a fixed number of items, regardless of the value selected in the num_flags template variable.

  • What did you expect to happen?

I expected the table panel to adjust the number of displayed flags according to the value selected in the num_flags template variable.

  • Configuration:

Template Variable num_var:

  • Type: Custom
  • Values: 1, 2, 3, …, 50
  • Query: None

Query within Table Panel:

jsonCopy code

{
  "find": "terms",
  "field": "my_field",
  "size": "$num_var",
  "orderBy": "doc_count",
  "query": "my query"
}
  • Errors:
    No specific errors are displayed in the Grafana UI or related logs. The table panel just doesn’t seem to respond to changes in the num_flags variable.
  • Online Instructions:
    I followed the information provided in the official Grafana documentation about using variables in queries: URL

do you get anything if you add something manually?

{
  "find": "terms",
  "field": "my_field",
  "size": 10,
  "orderBy": "doc_count",
  "query": "my query"
}

Yes, when I use a manual number for size, it works perfectly. Note that the 10 needs to be in quotes “10” to work.

1 Like

maybe try this instead without double quotes?

${num_var:doublequote}

If I don’t use double quotes, then I get a json formatting exception:

Templating

Template variable service failed Unexpected token ‘$’, …“, “size”: ${num_var”… is not valid JSON

1 Like