Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

Nifi JOLT spec to add attribute only when parent attribute is present

Nifi JOLT spec to add attribute only when parent attribute is present

Hello,

I have a json input for which I need to add one key-value("correlationId": "123456789") under 'tripSummaryMetaData' section dynamically.

I tried with below spec and its working fine, but the issues is when 'tripSummaryMetaData' is not available the it is creating new 'tripSummaryMetaData' and adding my attribute.

When 'tripSummaryMetaData' is not available the it should not add my new attribute, is that possible in JOLT?

Thanks in advance.

Input:

{ "tripSummaryMetaData": { "orgId": "demo", "message_type_id": "MB-CD-MSG0001" }, "eventSummary": { "tripId": "2018072204cc7851d3e84f7ea8a1155c95f65b06", "referenceData": "050bfa62-430a-45a5-b7a6-78d688d45167", "gpsData": [{ "gpsTimeReceived": 1532319157338, "gpsTime": "2018-07-22T21:12:38-07:00" }] } }

Output:

{ "tripSummaryMetaData" : { "orgId" : "demo", "message_type_id" : "MB-CD-MSG0001", "correlationId" : "123456789" }, "eventSummary" : { "tripId" : "2018072204cc7851d3e84f7ea8a1155c95f65b06", "referenceData" : "050bfa62-430a-45a5-b7a6-78d688d45167", "gpsData" : [ { "gpsTimeReceived" : 1532319157338, "gpsTime" : "2018-07-22T21:12:38-07:00" } ] } }

Spec :

[{ "operation": "default", "spec": { "tripSummaryMetaData": { "correlationId": "123456789" } } }]

Thanks,

Mahendra

2 REPLIES 2

Re: Nifi JOLT spec to add attribute only when parent attribute is present

@Matt Burgess , @Shu- Your help would be appreciated. Thanks

Re: Nifi JOLT spec to add attribute only when parent attribute is present

Super Guru

@Mahendra Hegde

You can use EvaluateJsonPath processor to extract the value from $.tripSummaryMetaData.orgId then keep as attribute to the flowfile then use RouteOnAttribute processor to check the value of the attribute,

1.If empty then feed to doesn't feed the json message to Jolt transform processor.

2.if not empty then feed the relationship to add the default operation.

(or)

Use Update Record processor and configure Record Reader/Writer controller services(in writer keep supress null values to true) then using RecordPathvalue, check the value exists for the specific record path by using record path guide, then use If else function to add default value.

References regarding UpdateRecord and Record Path Guide

Don't have an account?
Coming from Hortonworks? Activate your account here