1
votes

I have a json that comes from InvokeHTTP. I did a Split Json and JoltTransform to get the Key, Values, but I need to change all the Keys from Camelcase to snakecase.

My Keys will be different with every InvokeHttp call. I've tried AttributestoJson and EvaluateJsonPath and some replace text, but haven't figured out a way to dynamically change just the keys and then merge back to the values without writing a custom processor.

Original Data from InvokeHTTP:

{
  "data": {
    "Table": [
      {
        "Age": 51,
        "FirstName": "Bob",
        "LastName": "Doe"
      },
      {
        "Age": 26,
        "FirstName": "Ryan",
        "LastName": "Doe"
      }
    ]
  }
}

Input after Split Json (Gives me each json in a separate flowfile) and Jolt:

[
  {
    "Key": "Age",
    "Value": 51
  },
  {
    "Key": "FirstName",
    "Value": "Bob"
  },
  {
    "Key": "LastName",
    "Value": "Doe"
  }
]

Desired Output:

{
  "data": {
    "Table": [
      {
        "age": 51,
        "first_name": "Bob",
        "last_name": "Doe"
      },
      {
        "age": 26,
        "first_name": "Ryan",
        "last_name": "Doe"
      }
    ]
  }
}
2
what about execute script ? - daggett
That is the route I figured out a few hours ago. Still having some problems getting that to work - user6754289

2 Answers

1
votes

I wrote an answer to a similar question which uses ReplaceText to replace . in JSON keys with _. The same logic could be applied here (template available in link). As I pointed out in that answer, a cleaner solution would be to use ExecuteScript, especially as the transformation from camelcase to snakecase is done easily in most scripting languages.

1
votes

If you know the fields, you can use JoltTransformJSON on the original input JSON so you don't have to use SplitJson, here's a spec that will do the (explicit) field name conversion:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "Table": {
          "*": {
            "Age": "data.Table[#2].age",
            "FirstName": "data.Table[#2].first_name",
            "LastName": "data.Table[#2].last_name"
          }
        }
      }
    }
  }
]

You could also use UpdateRecord, you'd just need separate schemas for the JsonTreeReader and JsonRecordSetWriter.