Support Questions

Find answers, ask questions, and share your expertise

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

avatar
Master Collaborator

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

avatar
Master Collaborator

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

avatar
Master 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