# JSLT

The **JSLT** connector allows manipulation of a JSON using JSLT, a language for JSON processing and querying. For more information, [see the official documentation on Github](https://github.com/schibsted/jslt).

The connector is useful to perform several actions, such as:

* Modify the structure of a JSON and keep its values.
* Add, extract, and remove data from a JSON.
* Sort the structure of a JSON.
* Modify the values contained in a JSON through functions, such as text manipulation, mathematical calculations, conversions between data types, among others.
* Access and manipulate data from arrays.

## **Parameters**

Take a look at the configuration parameters for the connector. Parameters supported by [Double Braces expressions](https://docs.digibee.com/documentation/connectors-and-triggers/double-braces/overview) are marked with `(DB)`.

### **General tab**

<table data-full-width="true"><thead><tr><th>Parameter</th><th>Description</th><th>Default value</th><th>Data type</th></tr></thead><tbody><tr><td><strong>Payload</strong> <code>(DB)</code></td><td>The JSON content to be manipulated.</td><td><code>{{ message.payload }}</code></td><td>JSON</td></tr><tr><td><strong>JSLT</strong></td><td>The JSLT declaration to be executed.</td><td><code>{ *: . }</code></td><td>JSON</td></tr><tr><td><strong>Fail On Error</strong></td><td>If enabled, interrupts the pipeline execution when an error occurs. If disabled, execution continues, but the <code>"success"</code> property will be set to <code>false</code>.</td><td>False</td><td>Boolean</td></tr><tr><td><strong>Raw Mode</strong></td><td><p>If this option is active, the JSLT expression can dynamically incorporate values using Double Braces (<code>{{ }}</code>) for entire expressions, object keys, or object values. This enables more flexible and dynamic transformations.</p><p></p><p><strong>Important:</strong> When using Double Braces for a JSON key within the <code>jsltExpr</code> and <strong>Raw Mode</strong> is <strong>enabled</strong>, you must escape the key. For example, to dynamically use <code>myKey</code> as a key, you would write <code>"{ \"{{message.myKey}}\": .value }"</code>.</p></td><td>False</td><td>Boolean</td></tr></tbody></table>

{% hint style="info" %}
The connector doesn’t support JSLT import statements.
{% endhint %}

### **Documentation tab**

<table data-full-width="true"><thead><tr><th>Parameter</th><th>Description</th><th>Default value</th><th>Data type</th></tr></thead><tbody><tr><td><strong>Documentation</strong></td><td>Optional field to describe the connector configuration and any relevant business rules.</td><td>N/A</td><td>String</td></tr></tbody></table>

## **JSLT expression examples**

### **Simple data transpose**

#### **Payload**

```json
{
  "id" : "w23q7ca1-8729-24923-922b-1c0517ddffjf1",
  "type" : "component",
  "prefix": "jslt",
  "myString": "test",
  "myArray": [ 1, 2, 3, 4],
  "boolean": true
}
```

#### **JSLT**

```json
{
  "uuid": .id,
  "obj": {
   "key-1": .type,
   "key-2": .prefix
  },
  * - myString, myArray, id: .
}
```

#### **Output**

```json
{
  "uuid" : "w23q7ca1-8729-24923-922b-1c0517ddffjf1",
  "obj" : {
    "key-1" : "component",
    "key-2" : "jslt"
  },
  "type" : "component",
  "prefix" : "jslt",
  "boolean" : true
}
```

### **JSLT Native functions**

{% hint style="info" %}
The example below represents some of the available JSLT native functions. For more information about it and the complete list of functions, [visit the Github documentation](https://github.com/schibsted/jslt/blob/master/functions.md).
{% endhint %}

#### **Payload**

```json
{
  "id" : "w23q7ca1-8729-24923-922b-1c0517ddffjf1",
  "type" : "component",
  "prefix": "jslt",
  "myString": "TEST",
  "myArray": [ 1, 2, 3, 4],
  "boolean": true
}

```

#### **JSLT**

```json
{
  "uuid": split(.id, "-"),
  "obj": {
   "key-1": uppercase(.type),
   "key-2": join(.myArray, lowercase(.myString))
  },
  * : .
}
```

#### **Output**

```json
{
  "uuid" : [ "w23q7ca1", "8729", "24923", "922b", "1c0517ddffjf1" ],
  "obj" : {
    "key-1" : "COMPONENT",
    "key-2" : "1test2test3test4"
  },
  "id" : "w23q7ca1-8729-24923-922b-1c0517ddffjf1",
  "type" : "component",
  "prefix" : "jslt",
  "myString" : "TEST",
  "myArray" : [ 1, 2, 3, 4 ],
  "boolean" : true
}
```

### **Variables and custom functions**

#### **Payload**

```json
{
  "id" : "w23q7ca1-8729-24923-922b-1c0517ddffjf1",
  "s1": "jslt",
  "s2": "component"
}
```

#### **JSLT**

```json
let splitted = split(.id, "-")
let size = size($splitted)

def concat(s1, s2)
  if ($s1 != null)
    $s1 + "___" + $s2
  else
    ""

{
  "variables": $splitted,
  "size": $size,
  "customFunction": concat(.s1, .s2)
}
```

#### **Output**

```json
{
  "variables": [
    "w23q7ca1",
    "8729",
    "24923",
    "922b",
    "1c0517ddffjf1"
  ],
  "size": 5,
  "customFunction": "jslt___component"
}
```

### **If and For Operators**

#### **Payload**

```json
{
  "id" : "w23q7ca1-8729-24923-922b-1c0517ddffjf1",
  "status": true
}
```

#### **JSLT**

```json
let aux = split(.id, "-")

def concat(string1, string2)
   $string1 + "-" + $string2

{
  "forResult": [for ($aux) concat("test", .)],
  "ifResult": if (.status)
                 size(.id)
              else
                 0
}

```

#### **Output**

```javascript
{
  "forResult" : [ "test-w23q7ca1", "test-8729", "test-24923", "test-922b", "test-1c0517ddffjf1" ],
  "ifResult" : 38
}

```

### **Dynamic expressions with Raw Mode**

When **Raw Mode** is enabled, you can dynamically construct parts of your JSLT expression using [Double Braces](https://docs.digibee.com/documentation/connectors-and-triggers/double-braces/overview) (`{{ }}`).

<details>

<summary><strong>Complete dynamic expression</strong></summary>

This example shows how to use a complete JSLT expression passed dynamically through the payload.

**Parameters**

* **Payload**: `{{ message.payload }}`
* **JSLT**: `{{ message.expression }}`
* **Raw Mode**: `true`

**Input Payload**

```json
{
  "payload": {
    "id" : "123456",
    "type": "String",
    "object": {
      "nested": "true"
    },
    "array": [
      99, 88, 77, 66, 55
    ]
  },
  "expression": "{  \\"code\\" : .id,  \\"boolean\\": .object.nested,  \\"element\\": .array[3],  \\"range\\": . array[0 : 2],   *: .}"
}
```

**Output**

```json
{
  "code": "123456",
  "boolean": "true",
  "element": 66,
  "range": [ 99, 88 ],
  "id": "123456",
  "type": "String",
  "object": {
    "nested": "true"
  },
  "array": [ 99, 88, 77, 66, 55 ]
}
```

</details>

<details>

<summary><strong>Dynamic object key</strong></summary>

This example demonstrates how to dynamically set an object key within the JSLT expression. Note the escaped double quotes for the dynamic key.

**Parameters**

* **Payload**: `{{ message.payload }}`
* **JSLT**: `{ "code" : .id, {{ message.key }}: .object.nested, "element": .array[3], "range": . array[0 : 2], *: .}`
* **Raw Mode**: `true`

**Input payload**

```json
{
  "payload": {
    "id" : "123456",
    "type": "String",
    "object": {
      "nested": "true"
    },
    "array": [
      99, 88, 77, 66, 55
    ]
  },
  "key": "\"boolean\""
}
```

**Output**

```json
{
  "code": "123456",
  "boolean": "true",
  "element": 66,
  "range": [ 99, 88 ],
  "id": "123456",
  "type": "String",
  "object": {
    "nested": "true"
  },
  "array": [ 99, 88, 77, 66, 55 ]
}
```

</details>

<details>

<summary><strong>Dynamic object value</strong></summary>

This example shows how to dynamically set an object value within the JSLT expression.

**Parameters**

* **Payload**: `{{ message.payload }}`
* **JSLT**: `{ "code" : .id, "boolean": {{ message.value }}, "element": .array[3], "range": . array[0 : 2], *: .}`
* **Raw Mode**: `true`

**Input payload**

```json
{
  "payload": {
    "id" : "123456",
    "type": "String",
    "object": {
      "nested": "true"
    },
    "array": [
      99, 88, 77, 66, 55
    ]
  },
  "value": ".object.nested"
}
```

**Output**

```json
{
  "code": "123456",
  "boolean": "true",
  "element": 66,
  "range": [ 99, 88 ],
  "id": "123456",
  "type": "String",
  "object": {
    "nested": "true"
  },
  "array": [ 99, 88, 77, 66, 55 ]
}
```

</details>

<details>

<summary><strong>Dynamic object key and value</strong></summary>

This example demonstrates using Double Braces for both a dynamic key-value pair (`*: .`) within the JSLT expression.

**Parameters**

* **Payload**: `{{ message.payload }}`
* **JSLT**: `{ "code" : .id, "boolean": {{ message.value }}, "element": .array[3], "range": . array[0 : 2], {{ message.keyAndValue }}}`
* **Raw Mode**: `true`

**Input payload**

```json
{
  "payload": {
    "id" : "123456",
    "type": "String",
    "object": {
      "nested": "true"
    },
    "array": [
      99, 88, 77, 66, 55
    ]
  },
  "value": ".object.nested",
  "keyAndValue": "*:."
}
```

**Output**

```json
{
  "code": "123456",
  "boolean": "true",
  "element": 66,
  "range": [ 99, 88 ],
  "id": "123456",
  "type": "String",
  "object": {
    "nested": "true"
  },
  "array": [ 99, 88, 77, 66, 55 ]
}
```

</details>
