String interpolation

Flux string interpolation evaluates string literals containing one or more placeholders and returns a result with placeholders replaced with their corresponding values.

String interpolation syntax

To use Flux string interpolation, enclose embedded expressions in a dollar sign and curly braces ${}. Flux replaces the content between the braces with the result of the expression and returns a string literal.

  1. name = "John"
  2. "My name is ${name}."
  3. // My name is John.

Flux only interpolates string values

Flux currently interpolates only string values (IMP#1775). Use the string() function to convert non-string values to strings.

  1. count = 12
  2. "I currently have ${string(v: count)} cats."

Use dot notation to interpolate record values

Records consist of key-value pairs. Use dot notation to interpolate values from a record.

  1. person = {
  2. name: "John",
  3. age: 42
  4. }
  5. "My name is ${person.name} and I'm ${string(v: person.age)} years old."
  6. // My name is John and I'm 42 years old.

Flux returns each record in query results as a record. In Flux row functions, each row record is represented by r. Use dot notation to interpolate specific column values from the r record.

Use string interpolation to add a human-readable message
  1. from(bucket: "example-bucket")
  2. |> range(start: -30m)
  3. |> map(fn: (r) => ({
  4. r with
  5. human-readable: "${r._field} is ${r._value} at ${string(v: r._time)}."
  6. }))

String interpolation versus concatenation

Flux supports both string interpolation and string concatenation. String interpolation is a more concise method for achieving the same result.

  1. person = {
  2. name: "John",
  3. age: 42
  4. }
  5. // String interpolation
  6. "My name is ${person.name} and I'm ${string(v: person.age)} years old."
  7. // String concatenation
  8. "My name is " + person.name + " and I'm " + string(v: person.age) + " years old."
  9. // Both return: My name is John and I'm 42 years old.

Check and notification message templates configured in the InfluxDB user interface do not support string concatenation.