# switch

## Definition

In a config modifier schema, use `switch` to check the value of a path against possible values that require different outcomes (i.e., returning a constant or parsing a different value). 

> **Exclusive keywords**
>
> If `properties`, `if`, `concat`,  `switch`, `pipe`, `merge`, `prefer`, or `items` are present at the same nesting level, only the first keyword listed will run. We recommend not listing these keywords together at the same nesting level. 

## Order of execution

This keyword executes in the following order: 

1. `omit`
2. `constant`
3. `references`
4. `use`
5. `get`
6. Mutually exclusive keywords at this level:
   1. `properties` 
   2. `if` 
   3. `concat` 
   4. **`switch`** 
   5. `pipe` 
   6. `merge` 
   7. `prefer` 
   8. `items`
7. `default`
8. `plugin`

## Example

You can look at every observation code and shift the value from `0` to `1` for certain codes. If the code matches one of the cases, the schema then checks the observation’s `Value` against a nested `switch` statement. 

**Example: Switch input from initial payload**

```json
{
  "Observations": [
    {
      "DateTime": "2025-06-26T22:15:15.969369",
      "Value": "1",
      "ValueType": "Numeric",
      "Units": null,
      "Code": "68520-6",
      ...
    },
    {
      "DateTime": "2025-06-26T22:15:15.969369",
      "Value": "2",
      "ValueType": "Numeric",
      "Units": null,
      "Code": "66864-0",
      ...
    },
    {
      "DateTime": "2025-06-26T22:15:15.969369",
      "Value": "3",
      "ValueType": "Numeric",
      "Units": null,
      "Code": "66867-3",
      ...
    }
  ]
}
```

**Example: Switch selector**

```json
$.Observations[*].Value
```

For instance, if `Observations[0].Code` is `68520-6`, it matches the second case. The nested switch then uses `select: {}` to look back at the original selector (`$.Observations[*].Value`). Because the `Value` is `1`, the nested switch maps it to `2`.

**Example: Config modifier with switch keyword**

```yaml
switch:
  select:
    use: '@parent'
    get: Code
  cases:
    - terms:
        - constant: 68525-5
        - constant: 68526-3
        - constant: 68527-1
        - constant: 68528-9
        - constant: 68529-7
        - constant: 68530-5
        - constant: 68531-3
        - constant: 68532-1
        - constant: 68533-9
        - constant: 68534-7
      then:
        switch:
          select: {}
          cases:
            - terms:
                - constant: '0'
              then:
                constant: '2'
          else:
            comment: do nothing
    - terms:
        - constant: 68518-0
        - constant: 68519-8
        - constant: 68520-6
        - constant: AUDITCQ1
        - constant: AUDITCQ2
        - constant: AUDITCQ3
      then:
        switch:
          select: {}
          cases:
            - terms:
                - constant: '0'
              then:
                constant: '1'
            - terms:
                - constant: '1'
              then:
                constant: '2'
            - terms:
                - constant: '2'
              then:
                constant: '3'
            - terms:
                - constant: '3'
              then:
                constant: '4'
            - terms:
                - constant: '4'
              then:
                constant: '5'
          else:
            comment: do nothing
    - terms:
        - constant: 102011-4
        - constant: 102012-2
        - constant: 102013-0
        - constant: 102014-8
        - constant: 102015-5
        - constant: 102016-3
      then:
        switch:
          select: {}
          cases:
            - terms:
                - constant: '0'
              then:
                constant: '2'
          else:
            comment: do nothing
  else:
    comment: do nothing
```

Based on this example config modifier, the `Value` is now `2`.

**Example: Switch output from processed payload**

```json
{
  "Observations": [
    {
      "DateTime": "2025-06-26T22:15:15.969369",
      "Value": "2",
      "ValueType": "Numeric",
      "Units": null,
      "Code": "68520-6",
      ...
    },
    {
      "DateTime": "2025-06-26T22:15:15.969369",
      "Value": "2",
      "ValueType": "Numeric",
      "Units": null,
      "Code": "66864-0",
      ...
    },
    {
      "DateTime": "2025-06-26T22:15:15.969369",
      "Value": "3",
      "ValueType": "Numeric",
      "Units": null,
      "Code": "66867-3",
      ...
    }
  ]
}  
```
