Support Questions

Find answers, ask questions, and share your expertise

How to do JOLT replace on all JSON keys in Nifi

avatar
Super Collaborator

Hi,

I have a requirement for transforming JSON on Nifi which seems simple but I haven't been able to solve:

Input json:

{
	"agent-submit-time" : -1,
	"agent-end-time" : 123445,
	"agent-name" : "Marie Bayer-Smith"
}

Desired:

{
	"agent_submit_time" : -1,
	"agent_end_time" : 123445,
	"agent_name" : "Marie Bayer-Smith"
}

-I don't want to use the ReplaceText processor since replacing "-" for "_" might impact values too

-I need this to be able to infer AVRO schema on incoming JSON records (AVRO does not like the dashes at all)

-since I already use a Jolt processor for another transformation in the JSON it makes sense to include it in the same processor to prevent unnecessary Nifi overhead.

I think I would need the JoltTransformJSON processor as it is very powerful (but the syntax evades me) for this but open for other options too.

1 ACCEPTED SOLUTION

avatar
Master Guru

You can use the following JOLT spec in the JoltTransformJSON processor:

[
  {
    "operation": "shift",
    "spec": {
      "*-*-*": "&(0,1)_&(0,2)_&(0,3)",
      "*-*": "&(0,1)_&(0,2)",
      "*": "&"
    }
  }
]

Note that you have to add an entry for each "level" of name matching, so if you could have elements with 4 dashes, you'd need an extra line at the top, following the same pattern.

View solution in original post

10 REPLIES 10

avatar
New Contributor

Thanks @Matt Burgess

I will try it and let you know if it gives problems.