---
title: Formula count – Count Extracted Values in Nodinite Search Field Expressions
description: The count function in the Nodinite Formula plugin returns how many values the content holds. Syntax, the unique parameter, and tested examples with xPath, jsonPath, message body, and message context.
---

# :fa-function: Formula - Count

The `count` function, one of the Math functions in the [Formula][] plugin for [Search Field Expressions][], returns how many values the content holds.

![Count the order lines of an Order Entry XML, then add up their line totals with sum, animated walkthrough (7.12)][2]  
*Animated walkthrough: on a three-line Order Entry XML of the [Order-to-Cash demo][], `count(xPath('OrderEntry/OrderLines/OrderLine/LineId', body))` returns `3`. The same order then shows [sum][] of the line totals: `1022.80`.*  

## :fa-code: Syntax

```text
count(content [, unique])
```

### Parameters

| Parameter | Required | Description |
| --- | --- | --- |
| `content` | ✅ Yes | The values to count: [body][], [context][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 number: how many values `content` holds.
* Duplicates are counted once by default, because `unique` defaults to `true` for `count` (see [Example 5](#example-5-count-values-that-contain-duplicates)). Use `count(..., false)` to count every occurrence.

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

## :fa-list-ol: Examples

### Example 1: Count the orders in an XML message

#### Input

Message Body:

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

#### Formula

```text
count(xPath('Orders/Order/Id', body))
```

#### Result

```text
2
```

[xPath][] returns the two order ids, `1337` and `1338`.

![Test Expression tab: count(xPath('Orders/Order/Id', body)) on a message with two orders returns 2 (7.12)][ex1]  
*Counting the `Id` elements gives the number of orders in the message, here `2`.*  

### Example 2: Count the message body

#### Input

Message Body:

```text
Nodinite
```

#### Formula

```text
count(body)
```

#### Result

```text
1
```

![Test Expression with count(body) on the message body Nodinite, returning 1][1]  
*The whole message body is one value, so `count` returns `1`.*  

### Example 3: Count a message context value

#### Input

Message Context:

| Key | Value |
| --- | --- |
| `id` | `Nodinite` |

#### Formula

```text
count(context('id'))
```

#### Result

```text
1
```

![Test Expression with count(context('id')) on the context key id with value Nodinite, returning 1][6]  
*The context key `id` holds one value, so `count` returns `1`.*  

### Example 4: Count the values extracted from JSON

#### Input

Message Body:

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

#### Formula

```text
count(jsonPath('a[*].b', body))
```

#### Result

```text
3
```

![Test Expression with count of jsonpath over every b in the array a, on a JSON array with three values, returning 3][3]  
*[jsonPath][] extracts `This`, `is`, and `Formula Plugin`, so `count` returns `3`.*  

### Example 5: Count values that contain duplicates

#### Input

Message Body:

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

#### Formula

```text
count(jsonPath('a[*].b', body))
```

#### Result

```text
3
```

![Test Expression with count of jsonpath over every b in the array a, on a JSON array where the value is appears twice, returning 3][7]  
*The array holds four values, but `is` appears twice and is counted once, so `count` returns `3`.*  

### Example 6: Count values across several message context keys

#### Input

Message Context:

| Key | Value |
| --- | --- |
| `id` | `<Orders><Id>3</Id><Id>6</Id></Orders>` |
| `id2` | `<Orders><Id>4</Id><Id>5</Id></Orders>` |

#### Formula

```text
count(xPath('/Orders/Id', context(regex('id.*'))))
```

#### Result

```text
4
```

![Test Expression with count(xpath('/Orders/Id', context(regex('id.*')))) on two context keys holding XML, returning 4][8]  
*`context(regex('id.*'))` returns both XML values, and [xPath][] extracts two ids from each, so `count` returns `4`.*  

## :fa-circle-info: Notes

* When the content holds no values, `count` returns `0`, a value, not the warning *The Expression yields no result*.
* Point [xPath][] at a leaf element, such as `.../OrderLine/LineId`. An element with child elements (`.../OrderLine`) gives no values, so `count(xPath('OrderEntry/OrderLines/OrderLine', body))` returns `0`.

## :fa-hand-point-right: Next Steps

* :fa-magnifying-glass-plus: [Add or manage Search Field][]  
* :fa-flask: [Test Your Expression][]  
* :fa-hard-drive: [Add or manage Log View][]  

## :fa-link-horizontal: Related Topics

* :fa-calculator: [avg][average]  
* :fa-text-width: [length][]  
* :fa-arrow-up-wide-short: [max][]  
* :fa-arrow-down-short-wide: [min][]  
* :fa-circle-half-stroke: [round][]  
* :fa-plus: [sum][]  
* :fa-function: [Formula][]  
* :fa-scroll: [Formula language rules][]  
* :fa-puzzle-piece: [Search Field Expressions][]  

[1]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Math/CountSimpleBody.png
[2]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Math/Formula_SumCount.gif
[3]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Math/CountJsonpath.png
[6]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Math/CountSimpleMessageContext.png
[7]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Math/CountJsonpathDuplicatedValue.png
[8]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Math/CountXpath%28regex%29MessageContext.png

[Add or manage Log View]:{ndr}/Administration/1.%20Log%20Management/1.%20Log%20Views/Features/Add%20or%20Manage%20Log%20View.md
[Add or manage Search Field]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Add%20or%20manage%20Search%20Field.md
[average]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/average.md
[body]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Source/body.md
[Context]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Source/context.md
[ex1]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Math/count_Example1.png
[Formula]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Overview.md
[Formula language rules]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Overview.md#fa-scroll-formula-language-rules
[jsonPath]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/jsonPath.md
[length]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/length.md
[max]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/max.md
[min]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/min.md
[Order-to-Cash demo]:{ndr}/Repository%20Model/BPMs/Examples/Order-to-Cash/Generate-O2C-Demo-Data.md
[round]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/round.md
[Search Field Expressions]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Overview.md
[sum]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Math/sum.md
[Test Your Expression]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Add%20or%20manage%20Search%20Field.md#test-your-expression
[xPath]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/xPath.md