# Payload variables

## Introduction

There may be times when you need to define variables or parameters for a process flow shape which resolve dynamically, based on given values from the incoming payload.

For example, you might have a list of customer IDs coming in from a source (for example, an inbound API job), and need to match these IDs with payload data for a subsequent connection shape, in order to create customer records. This would be achieved using a specific [payload syntax](#payload-variable-syntax) when defining variables in the [connection shape](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/connector-shape).  &#x20;

## Payload variable syntax

To pass in a variable or parameter value from the incoming payload, use the syntax shown below:

```
[[payload.<schema notation>]]
```

...where `schema notation` should be replaced with the relevant notation path for the required field in the incoming schema. A payload variable can be defined on its own, or combined with static text. For example:

```
prefix-[[payload.<schema notation>]]-suffix
```

If necessary, you can combine payload variables with metadata and/or flow variables - for example:

```
prefix-[[payload.<schema notation>]]-[[meta.unique_key]-suffix
```

## Supported

The `[[payload]]` variable supports non-JSON payload data types - for example, raw text, CSV, XML - whatever you pass in will be output.&#x20;

## Payload variable examples

To show how this works in principle, some examples are detailed below:

* [Single item](#example-single-item)
* [Single item in an array](#example-single-item-in-an-array)
* [All items in an array](#example-all-items-in-an-array)
* [Batched items from an array](#example-batched-items-from-an-array)

However, it's important to note that the required settings will depend on the data schemas used for your selected connection endpoints.&#x20;

### Example: single item

At the most basic level, your incoming data might contain items which aren't nested - for example:

```json
{
 "customerID":1000001
}
```

In this case, our variable would be defined as:

```
[[payload.customerID]]
```

...as shown below:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FDqwOb5DvwmZGh1CCEauL%2Fparameter%20from%20payload%202.png?alt=media&#x26;token=534b7b61-380c-4c50-8521-4d0f4237ba4a" alt="" width="375"><figcaption></figcaption></figure></div>

The result for our example would be:

```json
{
 1000001
}
```

### Example: all items in an array

An incoming payload might contain the required element in an array and you want to target **all items within it** - for example:

<pre class="language-json"><code class="lang-json"><strong>{
</strong>	"users": [{
			"customerID": "000001",
			"name": "Rita"
		},
		{
			"customerID": "000002",
			"name": "Bob"
		},
		{
			"customerID": "000003",
			"name": "Sue"
		}
	]
}
</code></pre>

In this case, we can define a variable to target the required array. For example:

```
[[payload.*.customerID]]
```

...will produce a comma separated list of associated `customerID` values. The result for our example would be:

```json
000001, 000002, 000003
```

### Example: single item in an array

An incoming payload might contain the required element in an array and you only want to target **a single item** - for example:

<pre class="language-json"><code class="lang-json"><strong>{
</strong>	"users": [{
			"customerID": "000001",
			"name": "Rita"
		},
		{
			"customerID": "000002",
			"name": "Bob"
		},
		{
			"customerID": "000003",
			"name": "Sue"
		}
	]
}
</code></pre>

In this case, we can define a variable to target the required array item. For example:

```
[[payload.users.0.customerID]]
```

...will target the **first** item in the array and return the associated `customerID` value. The result for our example would be:

```json
000001
```

Whereas:

```
[[payload.users.1.customerID]]
```

...will target the **second** item in the array and return the `customerID` value. The result for our example would be:

```json
000002
```

### Example: batched items from an array

Let's take our example below:

```json
{
	"users": [{
			"customerID": "000001",
			"name": "Rita"
		},
		{
			"customerID": "000002",
			"name": "Bob"
		},
		{
			"customerID": "000003",
			"name": "Sue"
		}
	]
}
```

We've seen how we can target [specific items in an array](#example-single-item), and how we can [list all items in an array](#example-all-items-in-an-array) - but what if we wanted to target all array items individually?&#x20;

In this case, we would add a [flow control](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/flow-control-shape) shape to split the incoming payload into batches of `1` at the required data element level (in the case of our example this would ne `users` :

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FUx54rCwqo1XE3OQW2w5U%2Fvariables%20from%20payload%203.png?alt=media&#x26;token=166f4e78-0482-47f5-bcf3-41654ba42f56" alt="" width="375"><figcaption></figcaption></figure></div>

This will result in three payloads, each with a single `customerID` and `name`.  For example:

```json
[{"customerID":"000001","name":"Rita"}] 
```

```json
[{"customerID":"000002","name":"Bob"}] 
```

```json
[{"customerID":"000003","name":"Sue"}] 
```

In the following connection step (where the variable/parameter is defined), we can add our payload syntax for the variable/parameter. For our example this would be:

```json
[[payload.*.customerID]]
```

...as shown below:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FdaOvVzNICUhEjU159Fdx%2Fvariables%20from%20payload%204.png?alt=media&#x26;token=d43c04f5-d398-4c73-a023-0fbcff94c16f" alt="" width="375"><figcaption></figcaption></figure></div>

Here, we need to specify the \* because each of our batched payloads is wrapped in an array. For each payload generated from our example, the result is that the associated `customerID` is taken as the variable value.&#x20;

## Related information

* [Metadata variables](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/dynamic-variables/metadata-variables)
* [Flow variables](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/dynamic-variables/flow-variables)
* [Generating dynamic cache keys with variables](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/advanced-shapes/cache/add-to-cache-shape/generating-dynamic-cache-keys-with-variables)
