- 2 minutes to read

Formula - Length

The length function, one of the Math functions in the Formula plugin for Search Field Expressions, returns the character count of each value in the content.

Syntax

length(content [, unique])

Parameters

Parameter Required Description
content ✅ Yes The values to measure: body, context, or another Formula function such as xPath or jsonPath.
unique ❌ No Unquoted true or false; default true for this function. false: a value that appears several times counts every time. true: identical values count once. Values are compared as text, so 100 and 100.00 are different values. A quoted 'true' is an error.

Returns

  • One character count per value in content; several input values give several counts (see Example 4).
  • To get a single number from several counts, wrap length in max, min, sum, or avg (see max).

Quoting, nesting, and how content is resolved are described in the Formula language rules.

Examples

Example 1: Measure a city name in an XML order

Input

Message Body:

<ns0:Orders xmlns:ns0="SupplyChain.Schemas/1.0">
  <Order>
    <Id>1337</Id>
    <Amount>666</Amount>
    <City>New York</City>
    <Partner>QWNtZQ==</Partner>
  </Order>
</ns0:Orders>

Formula

length(xPath('Orders/Order/City', body))

Result

8

New York is eight characters long, including the space.

Test Expression tab: length(xPath('Orders/Order/City', body)) on an order with City New York returns 8
length measures the extracted text, not the XML element, so the tags don't count.

Example 2: Measure the message body

Input

Message Body:

Nodinite

Formula

length(body)

Result

8

Test Expression with length(body) on the message body Nodinite, returning 8
Nodinite has eight characters, so length returns 8.

Example 3: Measure a message context value

Input

Message Context:

Key Value
Correlation Id demo001
Header#Content-Type Application/XML

Formula

length(context('Correlation Id'))

Result

7

Test Expression with Length(Context('Correlation Id')) on the context value demo001, returning 7
Only the Correlation Id key is read; its value demo001 has seven characters. The screenshot writes the function names as Length and Context; names are case-insensitive.

Example 4: Measure each value extracted from JSON

Input

Message Body:

{a:[{b:'This'},{b:'is'},{b:'Formula Plugin'}]}

Formula

length(jsonPath('a[*].b', body), true)

Result

4
2
14

Test Expression with Length of jsonpath over every b in the array a, with unique true, returning 4, 2, and 14
jsonPath extracts three values, and length returns one count for each: This (4), is (2), and Formula Plugin (14).

Next Steps