jsonjslt

Apply mapping to subset of fields in single JSLT transform


I have json input like this:

{
  "sys_id": "651bc076db1b00107e032a9a4896192f",
  "xxx_operation": "update",
  "xxx_table_name": "customerservice_case"
  "case": "upd12CS0001001",
  "short_description": "upd12"
}

There are 4 types of tables, defined in xxx_table_name

I would want to apply different mappings depending on the value of xxx_table_name. Also, some fields (like xxx_operation and sys_id) are common to all types of jsons.

What would be the proper way to do this transform with JSLT?

Ideally there would be transformations for the common fields and then specific transformations for the rest of the fields. Pseudocode:

{
    "operation": .xxx_operation, 
    "id": .sys_id,
    if (.xxx_operation == 'customerservice_case') {
        // customerservice_case specific transformations here
    }
    else if (.xxx_operation == 'customerservice_ticket') {
        // customerservice_ticket specific transformations here
    }
}

Thanks


Solution

  • You can merge together JSON objects using the + operator, so the custom code you have almost works. If you do this:

    {
        "operation": .xxx_operation, 
        "id": .sys_id
    } +
    if (.xxx_operation == 'customerservice_case') {
        // customerservice_case specific transformations here
    }
    else if (.xxx_operation == 'customerservice_ticket') {
        // customerservice_ticket specific transformations here
    }
    

    it will work. (I'm assuming the tests should really be on the value of xxx_table_name, but no matter.)