Convert JSON keys from camelCase to SNAKE_CASE
Today, I had to support syslog messages in two formats using a single mapping block:
- JSON Format – where keys are in
camelCase
- Syslog Format – where keys are in
SNAKE_CASE
To solve this, I came up with a Benthos preprocessor that converts camelCase
keys to snake_case
, then uppercases them. Here's the logic I used:
map camel_to_snake {
root = this.re_replace_all("([a-z])([A-Z])", "${1}_${2}").lowercase()
}
root = this.map_each_key(ele -> ele.apply("camel_to_snake")).uppercase()
Input
{
"actionName": "allowed",
"computerName": "test.example.com",
"destinationIp": "127.0.0.1"
}
Output
{
"ACTION_NAME": "allowed",
"COMPUTER_NAME": "test.example.com",
"DESTINATION_IP": "127.0.0.1"
}
Post a Comment