All Collections
Components
Transformer - operations overview
Transformer - operations overview

Overview with examples of each operation that can be used in the transformer (JOLT)

Erick Rubiales avatar
Written by Erick Rubiales
Updated over a week ago


IMPORTANT: This documentation has been discontinued. Read the updated Transformer - operations overview documentation on our new documentation portal.

This article presents some "operation" examples at the jolt.

BASICS OF JOLT:

operation (string): shift, sort, cardinality, modify-overwrite-beta, or remove.

shift: To change JSON values or parts of the input tree, and add them to specified locations at the output. The structure is summarized into browsing up to the variable or Json object that is intended to change the structure, place : (colon) and between "" quotation marks, inform the object destination. 

  • Refer to "all elements" within an object. It is possible to use * (asterisk).

  • Copy the variable name to the destination, use &

  • Create levels at the destination Json, use "." (dot)

Example: 

Input:

{
  "body": {
    "zip": "05350-000",
    "streetaddress": "Avenida Escola Politécnica",
    "neighbourhood": "Rio Pequeno",
    "city": "São Paulo",
    "state": "SP"
  },
  "client": {
    "code": 2,
    "name": "RODRIGO LARA",
    "email": "mail@digibee.com.br",
    "due_date": "2019-03-27"
  }
}

Spec:

[

  {
    "operation": "shift",
    "spec": {
      "client": {
        "code": "customer.id",
        "name": "customer.&",
        "email": "customer.email"
      },
      "body": {
        "*": "customer.address.&"
      }
    }
    }

]

Output:

{
  "customer" : {
    "id" : 2,
    "name" : "RODRIGO LARA",
    "email" : "mail@digibee.com.br",
    "address" : {
      "zip" : "05350-000",
      "streetaddress" : "Avenida Escola Politécnica",
      "neighbourhood" : "Rio Pequeno",
      "city" : "São Paulo",
      "state" : "SP"
    }
  }
}

default: To add new fields and values to the output JSON.
Example:

Input:

{
     "counterTop": {
       "loaf1": {
         "type": "white"
       },
       "loaf2": {
         "type": "wheat"
       },
       "jar1": {
         "contents": "peanut butter"
       },
       "jar2": {
         "contents": "jelly"
       }
}

Spec:

 [
     {
       "operation": "default",
       "spec": {
         "counterTop": {
           "loaf1": {
             "slices": [
               "slice1",
               "slice2",
               "slice3",
               "slice4
             ]
           }
         }
       }
     }
   ]
 }

Output:

{
   "counterTop" : {
    "loaf1" : {
      "type" : "white",
       "slices" : [ "slice1", "slice2", "slice3", "slice4" ]
    },
    "loaf2" : {
      "type" : "wheat"
    },
    "jar1" : {
       "contents" : "peanut butter"
    },
    "jar2" : {
       "contents" : "jelly"
    }
  }
}

cardinality: It transforms elements at the input JSON into unique values (object) or output lists (array).
Example:

Input:

{
   "counterTop": {
    "loaf1": {
       "type": "white",
       "slices": [
         "slice1",
        "slice2",
         "slice3",
         "slice4"
      ]
    },
    "loaf2": {
       "type": "wheat"
    },
    "jar1": {
       "contents": "peanut butter"
    },
    "jar2": {
       "contents": "jelly"
    }
  }
}

Spec:

[
   {
      "operation": "cardinality",
     "spec": {
        "counterTop": {
          "loaf1": {
            "slices": "ONE"
         }
       }
     }
    }
  ]

Rem. The "ONE" instruction changes its list to an object with the first list element, while the "MANY" instruction changes a JSON object to a list.

Output:

{
   "counterTop" : {
    "loaf1" : {
      "type" : "white",
       "slices" : "slice1"
    },
    "loaf2" : {
      "type" : "wheat"
    },
    "jar1" : {
       "contents" : "peanut butter"
    },
    "jar2" : {
       "contents" : "jelly"
    }
  }
}

Remove: Used for removing input JSON fields
Example:

Input:

{
   "counterTop": {
    "loaf1": {
       "type": "white",
       "slices": "slice1"
    },
    "loaf2": {
       "type": "wheat"
    },
    "jar1": {
       "contents": "peanut butter"
    },
    "jar2": {
       "contents": "jelly"
    }
  }
}

Spec:

[
   {
      "operation": "remove",
     "spec": {
        "counterTop": {
          "loaf2": "",
          "jar1": ""
       }
     }
    }
]

Output:

{
   "counterTop" : {
    "loaf1" : {
      "type" : "white",
       "slices" : "slice1"
    },
    "jar2" : {
      "contents" : "jelly"
    }
  }
}

modify-overwrite-beta: It allows using predefined functions at 'jolt' to change values and even the type of elements.
Functions include basic string and mathematic operations (toLower, toUpper, concat, min / max / abs, toInteger, toDouble, toInt, and can be applied to the origin JSON values.
Example:

Input:

{
   "counterTop": {
    "loaf1": {
       "type": "white",
       "slices": "slice1"
    },
    "jar2": {
       "contents": "jelly"
    }
  }
}

Spec:

[
  {
     "operation": "modify-overwrite-beta",
    "spec": {
       "counterTop": {
         "jar2": {
             //acessando o Elemento contents e alterando seu valor para upper
           "contents": "=toUpper"
        }
      }
    }
  }
]

Output:

{
  "counterTop" : {
    "loaf1" : {
      "type" : "white",
       "slices" : "slice1"
    },
    "jar2" : {
      //a saída em UpperCase
       "contents" : "JELLY"
    }
  }
}

sort: It sorts the entire JSON input at the output. Sorting cannot be configured; the entire JSON will be affected.

  • Sorting does not consider variable amounts, only its name.

  • The Output will be alphabetically sorted. (Remark: Following the JSON structure convention, the variable order does not change their structure/behavior)

Example:

Input:

{
   "counterTop": {
    "loaf1": {
       "type": "white",
       "slices": "slice1"
    },
    "jar2": {
       "contents": "JELLY"
    }
  }
}

Spec:

[
  {
     "operation": "sort"
    }
]

Output:

{
   "counterTop" : {
    "jar2" : {
       "contents" : "JELLY"
    },
    "loaf1" : {
       "slices" : "slice1",
      "type" : "white"
    }
  }
}


Remove all attributes with null value

[{
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  }]

Did this answer your question?