jkelley16
jkelley16

Reputation: 25

Merge separate flow files with separate JSON together in Nifi

I am attempting to merge two flowfiles together using MergeContent. The content of both files are JSON objects which I would like to append to each other and wrap both with a JSON key.

File 1:

"Create":[
 {
   "Action": "Create",
   "Book": {
       "Id": "1234"
    }
 },
 {
   "Action": "Create",
   "Video": {
       "Id": 3245
   }
 }
]

File 2:

"Update":[
 {
   "Action": "Update",
   "Book": {
       "Id": "5376"
    }
 },
 {
   "Action": "Update",
   "Video": {
       "Id": "8267"
   }
 }
]

I need the final output to look like so:

{
  "Transaction": {
    "Action": "Transaction",
    "Type": {
      "Create": [
        {fill in stuff}
      ],
      "Update": [
        {fill in stuff}
      ]      
    }
  }
}

What is happening is that my MergeContent processor is picking up more than one flow file out of the Update or Create input and it is merging 'like' flow files together, instead of merging the separate content together.

Screenshot of my MergeContent :

enter image description here

Upvotes: 1

Views: 435

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65408

You can add a MergeContent processor with

  • Delimiter Strategy = Text
  • Header = {
  • Footer = }
  • Demarcator = ,

in order to generate the core part

      "Create": [
        {fill in stuff}
      ],
      "Update": [
        {fill in stuff}
      ]  

then apply a JSONJoltTransform with specification

[
  {
    "operation": "shift",
    "spec": {
      "#Transaction": "&.Action",
      "@": "Transaction.Type"
    }
  }
]

in order to convert them to a formatted JSON value as desired such as

enter image description here

Upvotes: 0

Related Questions