bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.54k stars 328 forks source link

Need to remove spaces in a field and join the values #1201

Open Rishika-Shrivastava opened 1 year ago

Rishika-Shrivastava commented 1 year ago
{
  "metadata": {
    "timestamp": 1686748428069,
    "schemaVersion": 1,
    "type": "EVENT"
     },
  "data": {
    "event_name": "Menu item clicked",
    "tenant": "unspecified",
    "nw_type": "unspecified",
    "attrs": {
      "country_code": "IN",
      "coupon count": "NA",
      "menu item": "My Rides"
    }
  }
}

I am using the field event_name to create a kafka topic.

I need to remove all the spaces or replace them with underscores.

"event_name": "Menu item clicked" should look like "event_name": "Menuitemclicked" or "event_name": "Menu_item_clicked". How can I achieve that?

pushprajsinghjadoun commented 1 year ago

Hi, Here is the jolt transformation for your question.

jolt transformation:-

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "data": {
        "event_name1": "=split(' ',@(1,event_name))",
        "event_name": "=join('_',@(1,event_name1))"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "data": {
        "event_name1": ""
      }
    }
  }
]

Explanation:-

  1. In the first operation created the array of words using the split function and join the word with an underscore using the join function which makes a desired output string.
  2. In the second, remove the extra field which we created in the first operation using the remove operation.