Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Support Questions

Find answers, ask questions, and share your expertise
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!

Compare data within the JSON using NIFI

avatar
Rising Star

i have a JSON payload which contains latest and old orders. The data should be compared and output New, update, deleted. How can do it do that in NIFI?.

  "latest_orders": [
    { "order_id": "1001", "customer": "Alice", "amount": 250 },
    { "order_id": "1002", "customer": "Bob", "amount": 300 },
    { "order_id": "1004", "customer": "Diana", "amount": 150 }
  ],
  "old_orders": [
    { "order_id": "1001", "customer": "Alice", "amount": 200 },
    { "order_id": "1002", "customer": "Bob", "amount": 300 },
    { "order_id": "1003", "customer": "Charlie", "amount": 400 }
  ]
}
Output:
    { "order_id": "1001", "customer": "Alice", "amount": 250, "Action":"UPDATE" },
    { "order_id": "1002", "customer": "Bob", "amount": 300 ,"Action":"UPDATE"},
    { "order_id": "1004", "customer": "Diana", "amount": 150,"Action":"NEW" },
    { "order_id": "1003", "customer": "Charlie", "amount": 400,"Action":"DELETE" }
 
1 ACCEPTED SOLUTION

avatar
Master Guru

You can use ExecuteGroovyScript with the following script:

def ff = session.get()
if (!ff) return

def obj = new groovy.json.JsonSlurper().parse(ff.read())
def outObj = []

// Find updated records
def old_ids = obj.old_orders.collect {it.order_id}
def latest_ids = obj.latest_orders.collect {it.order_id}
old_ids.intersect(latest_ids).each {order_id ->
def update_order = obj.latest_orders.find {it.order_id == order_id}
update_order.Action = 'UPDATE'
outObj += update_order
}

// Find deleted records
(old_ids - latest_ids).each {order_id ->
def delete_order = obj.old_orders.find {it.order_id == order_id}
delete_order.Action = 'DELETE'
outObj += delete_order
}

// Find new records
(latest_ids - old_ids).each {order_id ->
def new_order = obj.latest_orders.find {it.order_id == order_id}
new_order.Action = 'NEW'
outObj += new_order
}
ff.write('UTF-8', groovy.json.JsonOutput.toJson(outObj))
REL_SUCCESS << ff

View solution in original post

3 REPLIES 3

avatar
Community Manager

Hi @steven-matison @MattWho @mburgess Do you have any insights here? Thanks!


Regards,

Diana Torres,
Senior Community Moderator


Was your question answered? Make sure to mark the answer as the accepted solution.
If you find a reply useful, say thanks by clicking on the thumbs up button.
Learn more about the Cloudera Community:

avatar
Master Guru

You can use ExecuteGroovyScript with the following script:

def ff = session.get()
if (!ff) return

def obj = new groovy.json.JsonSlurper().parse(ff.read())
def outObj = []

// Find updated records
def old_ids = obj.old_orders.collect {it.order_id}
def latest_ids = obj.latest_orders.collect {it.order_id}
old_ids.intersect(latest_ids).each {order_id ->
def update_order = obj.latest_orders.find {it.order_id == order_id}
update_order.Action = 'UPDATE'
outObj += update_order
}

// Find deleted records
(old_ids - latest_ids).each {order_id ->
def delete_order = obj.old_orders.find {it.order_id == order_id}
delete_order.Action = 'DELETE'
outObj += delete_order
}

// Find new records
(latest_ids - old_ids).each {order_id ->
def new_order = obj.latest_orders.find {it.order_id == order_id}
new_order.Action = 'NEW'
outObj += new_order
}
ff.write('UTF-8', groovy.json.JsonOutput.toJson(outObj))
REL_SUCCESS << ff

avatar
Community Manager

Hi @Althotta 

Have you been able to resolve your issue? If so, please mark the appropriate reply as the solution, as it will make it easier for others to find the answer in the future.

Screen Shot 2019-08-06 at 1.54.47 PM.pngmark as solution button

Keep the questions coming