> For the complete documentation index, see [llms.txt](https://docs.kasko.io/kasko-frontend-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.kasko.io/kasko-frontend-documentation/guides/transforming-request-data.md).

# Transforming request data

Sometimes there is need to change how what values and how are they being sent to API in for example quote or policy requests. Manifest request transformers can be used in cases like these.

Underneath [JsonLogic](/kasko-frontend-documentation/guides/jsonlogic.md) is used to figure out how to transform values. It is used both for `send_to_api` and `value`.

Transformer interface:

```typescript
interface {
  name: string,
  path: string | null,
  send_to_api?: boolean | ApiManifestJsonLogic,
  value?: boolean | string | number | null | ApiManifestJsonLogic
}
```

ApiManifestJsonLogic interface:

```typescript
interface {
  type: "jsonlogic",
  schema: JsonLogic
}
```

JsonLogic can access variables ([more info](http://jsonlogic.com/operations.html#var)). We expose 3 state slices for both `send_to_api` and `value` JsonLogic: `input`, `flags` and `quote`.

## Gotchas

There are some cases that might not be that clear initially:

* JsonLogic never returns `undefined` value, instead it returns `null`;
* Cannot use Objects in `value`, as [JsonLogic](/kasko-frontend-documentation/guides/jsonlogic.md) uses Object structure too and it doesn't allow any custom key/property;
  * Instead use dot notation in `path` to create nested structures like this:

```json
{
  "transformer": [
    {
      "name": "name",
      "path": "data.person",
      "send_to_api": true,
      "value": "John Wick"
    },
    {
      "name": "age",
      "path": "data.person",
      "send_to_api": true,
      "value": 21
    }
  ]
}
```

output for this would be:

```json
{
  "data": {
    "person": {
      "name": "John Wick",
      "age": 21
    }
  }
}
```

## Example with transformed value

Let's say the API needs `address` field, but user can tick `prefill_address` tickbox, that would change address from previously filled in address fields

Webapp manifest configuration fragment:

```json
{
  "version": "1.0",
  "schema": {
    "config": {
      "default_values": {
        "data.prefill_address": true,
        "data.risk_address": "Risk ave 20",
        "data.address": "Classic ave 1"
      },
      "requests": [
        {
          "type": "policy",
          "transformer": [
            {
              "name": "address",
              "path": "data",
              "send_to_api": true,
              "value": {
                "type": "jsonlogic",
                "schema": {
                  "if": [
                    {
                      "===": [
                        {
                          "var": "input.data.prefill_address"
                        },
                        true
                      ]
                    },
                    {
                      "var": "input.data.risk_address"
                    },
                    {
                      "var": "input.data.address"
                    }
                  ]
                }
              }
            }
          ]
        }
      ]
    },
    "flow": []
  }
}
```

In this example when Policy request is sent, it would look something like this:

```json
{
  "data": {
    "address": "Risk ave 20"
  }
}
```

And if there's already some data from `field_definition` it gets merged with transformed data. In case with some other data it would look like this (let's imagine that there is `address` and `risk_address` in `field_definition` too):

```json
{
  "first_name": "John",
  "last_name": "Wick",
  "data": {
    "risk_address": "Risk ave 20",
    "address": "Risk ave 20"
  }
}
```

## Example with conditional value

Let's say it's necessary to send value of `person_count` to Quote API service conditionally. When user is set `has_multiple_persons` to `true` then send value, if `has_multiple_persons` is `false` then do not send `person_count`.

Webapp manifest configuration fragment:

```json
{
  "version": "1.0",
  "schema": {
    "config": {
      "default_values": {
        "has_multiple_persons": true,
        "person_count": 2
      },
      "requests": [
        {
          "type": "quote",
          "transformer": [
            {
              "name": "person_count",
              "path": "",
              "send_to_api": {
                "type": "jsonlogic",
                "schema": {
                  "===": [
                    {
                      "var": "input.has_multiple_persons"
                    },
                    true
                  ]
                }
              },
              "value": {
                "type": "jsonlogic",
                "schema": {
                  "var": "input.person_count"
                }
              }
            }
          ]
        }
      ]
    },
    "flow": []
  }
}
```

In this example when Quote request is sent, it would look something like this:

```json
{
  "person_count": 2
}
```

And if there's already some data from `field_definition` it also gets merged same way like in previous example.

## Other useful example cases

* We can use string concatenation with [JsonLogic](http://jsonlogic.com/operations.html#cat):

```json
{
  "path": "data",
  "name": "date",
  "send_to_api": true,
  "value": {
    "type": "jsonlogic",
    "schema": {
      {
        "cat": [
          {
            "var": "input.data.date"
          }
          "T",
          {
            "var": "input.data.time"
          }
        ]
      }
    }
  }
}
```

* We can do some mathematics with [JsonLogic](hhttp://jsonlogic.com/operations.html#arithmetic-----):

```json
{
  "path": "data",
  "name": "discounted_price",
  "send_to_api": true,
  "value": {
    "type": "jsonlogic",
    "schema": {
      {
        "-": [
          {
            "var": "input.data.price"
          },
          {
            "var": "input.data.discount"
          }
        ]
      }
    }
  }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.kasko.io/kasko-frontend-documentation/guides/transforming-request-data.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
