Support Questions

Find answers, ask questions, and share your expertise

How to manipulate json key values using NiFI

avatar
New Contributor

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"
          }
        ]
      }
    }



1 REPLY 1

avatar
Master Guru

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.