Search code examples
jsonapache-nifijsonpath

Evaluate value of JSON Key using NIFI


I have a scenario where I can have multiple different types of json objects coming into my system. I do not know the object type ahead of time and based upon object type, will route to a different processor in my flow

{
  "book": {
    "id": "1234",
    "name": "book1"
  }
}

or

{
  "video": {
    "id": "3214",
    "name": "video1"
  }
}

or

{
  "magazine": {
    "id": "3233",
    "name": "magazine1"
  }
}

how can I evaluate if the object is a book, or a video, or a magazine to route to the correct processor

I've tried using evaluatejsonpath using the ~ but it just outputs the entire json object

Current flow :

enter image description here


Solution

  • One way is to extract all top level fields using EvaluateJsonPath processor, set the extracted field values to dynamic properties, and use the properties in RouteOnAttribute processor to route the flow to correct downstream processor.

    EvaluateJsonPath:

    enter image description here

    Please don't forget to set

    • 'Destination' to 'flowfile-attribute' and
    • 'Return Type' to 'json'

    If EvaluateJsonPath processor could not find the field or element, then the value of dynamic property will be set to empty string.

    All we need to do is to use the dynamic properties in RouteOnAttribute processor.

    RouteOnAttribute:

    Using equals() and not()

    enter image description here

    or using isEmpty() and not()

    enter image description here

    Please don't forget to set

    • 'Routing Strategy' to 'Route to Property Name'.

    Apache NiFi expression language guide

    Example Flow:

    enter image description here

    I am using PutFile processor as a downstream processor, for an example. It could be any processor.