> For the complete documentation index, see [llms.txt](https://doc.wearepatchworks.com/product-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/map-shape/mappings-tips-and-tricks/mapping-array-fields.md).

# Mapping array fields

## Introduction

This page provides guidance on mapping array fields, where the value from another field needs to be added to the array output.

To achieve this, wildcard (`*`) syntax is used to navigate and transform the JSON structure. Each asterisk represents a level in the data hierarchy, so you can copy values from parent objects into nested array elements without explicitly naming field names.

When an asterisk encounters an array, it means *apply to every element in the array*. This enables you to propagate data from higher levels down into all elements within nested arrays.

## Need to know

* Each asterisk (`*`) represents one level of depth in the JSON structure.
* When an asterisk encounters an array, it applies to every element in that array.
* Field names are used to navigate to specific elements in the structure.
* The pattern reads from left to right, with each component specifying the next\
  step in the traversal.
* Values are copied from higher levels down to lower levels, not moved or\
  deleted from their original location

## Example 1 (top-level array)

Consider the example below:

{% code lineNumbers="true" %}

```json
{
  "category": 1111,
  "fruit": [
    {
      "sku": "apples",
      "qty": 2
    },
    {
      "sku": "pears",
      "qty": 2
    }
  ]
}
```

{% endcode %}

We can map the `fruit` field very simply, as below:

<table data-card-size="large" data-view="cards"><thead><tr><th></th></tr></thead><tbody><tr><td><h4>Mapping rule</h4><p><img src="/files/bklkk8U1KSjblfoJWGQB" alt="" data-size="original"></p></td></tr><tr><td><h4>Output</h4><pre class="language-json" data-line-numbers><code class="lang-json">{
  "fruit": [
    {
      "sku": "apples",
      "qty": 2
    },
    {
      "sku": "pears",
      "qty": 2
    }
  ]
}
</code></pre></td></tr></tbody></table>

### The problem

Suppose we want to add the `category` field value to each item in the array, so the output is as shown below:

```json
{
  "fruit": [
    {
      "sku": "apples",
      "qty": 2,
      "category": 1111
    },
    {
      "sku": "pears",
      "qty": 2,
      "category": 1111
    }
  ]
}
```

### The solution

We can achieve this with the mapping rules below:

<figure><img src="/files/TU9DaGdiHZK3tHk2QPNe" alt=""><figcaption></figcaption></figure>

These rules can be summarised as follows:

<table><thead><tr><th width="255.19921875">Mapping rule</th><th>Summary</th></tr></thead><tbody><tr><td><code>fruit</code> ➤ <code>fruit</code></td><td>The field mapping sequence matters. Before we can add a field value to another element, that element must be mapped. In this example, we need to add the <code>category</code> value to each item in the <code>fruit</code> array, so we map the <code>fruit</code> field first.</td></tr><tr><td><code>category</code> ➤ <code>*.*.*.category</code></td><td>The field name to add/update in each item. Here we use <a href="#json-traversal">wildcards to traverse the JSON structure</a>.</td></tr></tbody></table>

#### JSON traversal

The `*.*.*.category` target mapping traverses the JSON structure as follows:

<table><thead><tr><th width="206.4609375">Expression</th><th>JSON traversal</th></tr></thead><tbody><tr><td>*</td><td>Start at the root: <code>{category: 1111, fruit:[...]}</code></td></tr><tr><td>*</td><td>Enter the <code>fruit</code> array.</td></tr><tr><td>*</td><td>Access every item in the <code>fruit</code> array: <code>{"sku": "apples","qty": 2},{"sku": "pears","qty": 2}</code></td></tr><tr><td>category</td><td>Add the <code>category</code> value (<code>1111</code>) to each array item.</td></tr></tbody></table>

## Example 2 (nested array)

Consider the example below:

{% code lineNumbers="true" %}

```json
{
  "category": 1111,
  "fruit": [
    {
      "sku": "apples",
      "qty": 2,
      "options":[{"type:":"pink lady"},{"type:":"granny smith"},{"type:":"russet"}]
    },
    {
      "sku": "pears",
      "qty": 2,
      "options":[{"type:":"conference"},{"type:":"blush gold"}]
    }
  ]
}
```

{% endcode %}

### The problem

Suppose we want to add the `category` field value to each item in the second array (`options`), so the output is as shown below:

```json
{
  "fruit": [
    {
      "sku": "apples",
      "qty": 2,
      "options": [
        {
          "type:": "pink lady",
          "category": 1111
        },
        {
          "type:": "granny smith",
          "category": 1111
        },
        {
          "type:": "russet",
          "category": 1111
        }
      ]
    },
    {
      "sku": "pears",
      "qty": 2,
      "options": [
        {
          "type:": "conference",
          "category": 1111
        },
        {
          "type:": "blush gold",
          "category": 1111
        }
      ]
    }
  ]
}
```

### The solution

We can achieve this with the mapping rules below:

<figure><img src="/files/6quVSI74HVUGNYZW3fHm" alt=""><figcaption></figcaption></figure>

These rules can be summarised as follows:

<table><thead><tr><th width="255.19921875">Mapping rule</th><th>Summary</th></tr></thead><tbody><tr><td><code>fruit</code> ➤ <code>fruit</code></td><td>The field mapping sequence matters. Before we can add a field value to another element, that element must be mapped. In this example, we need to add the <code>category</code> value to each item in the <code>fruit</code> array, so we map the <code>fruit</code> field first.</td></tr><tr><td><code>category</code> ➤ <code>*.*.*.options.*.category</code></td><td>The field name to add/update in each item. Here we use <a href="#json-traversal-1">wildcards to traverse the JSON structure</a>.</td></tr></tbody></table>

#### JSON traversal

The `*.*.*.options.*.category` target mapping traverses the JSON structure as follows:

<table><thead><tr><th width="206.4609375">Expression</th><th>JSON traversal</th></tr></thead><tbody><tr><td>*</td><td>Start at the root: <code>{category: 1111, fruit:[...]}</code></td></tr><tr><td>*</td><td>Enter the <code>fruit</code> array.</td></tr><tr><td>*</td><td>Access every item in the <code>fruit</code> array: <code>{"sku": "apples","qty": 2},{"sku": "pears","qty": 2, options: [...]}</code></td></tr><tr><td>options</td><td>Access the <code>options</code> array in each <code>fruit</code> item.</td></tr><tr><td>*</td><td>Apply to all items in the <code>options</code> array.</td></tr><tr><td>category</td><td>Add the <code>category</code> value (<code>1111</code>) to each <code>options</code> array item.</td></tr></tbody></table>

## Related pages

* [The array join transform function](/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/map-shape/working-with-field-transformations/available-transform-functions/array-transform-functions/array-join-transform-function.md)
