Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

apache nifi - I have two Json payload. I want to merge them in a single Json object

I have two payloads and want to merge them into single JSON object (streaming join). At few places people are suggesting to use AttributesToJSON, but as one of the JSON does not have fix set of attributes I guess that would not be possible.

First payload is

{  
   "title":"API-Actions Documentation",
   "title_link":"https://api.slack.com/",
   "author_name":"name",
   "author_link":"http://flickr.com/bobby/",
   "author_icon":"http://flickr.com/icons/bobby.jpg",
   "text":"Optional",
   "image_url":"http://my-website.com/path/to/image.jpg",
   "thumb_url":"http://example.com/path/to/thumb.png",
   "footer":null,
   "pretext":"@name",
   "color":"#7CD197"
}

And second one is,

{  
"fields":[  
  {  
     "title":"Priority",
     "value":"low",
     "short":"true"
  },
  {  
     "title":"Priority",
     "value":"medium",
     "short":"true"
  },
  {  
     "title":"Priority",
     "value":"high",
     "short":"true"
  },
  {  
     "title":"Priority",
     "value":"blocker",
     "short":"true"
  }
 ]
}

I want the output as

{   
"title":"API-Actions Documentation",
"title_link":"https://api.slack.com/",
"author_name":"name",
"author_link":"http://flickr.com/bobby/",
"author_icon":"http://flickr.com/icons/bobby.jpg",
"text":"Optional",
"image_url":"http://my-website.com/path/to/image.jpg",
"thumb_url":"http://example.com/path/to/thumb.png",
"footer":null,
"pretext":"@name",
"color":"#7CD197",
"fields":[  
  {  
     "title":"Priority",
     "value":"low",
     "short":"true"
  },
  {  
     "title":"Priority",
     "value":"medium",
     "short":"true"
  },
  {  
     "title":"Priority",
     "value":"high",
     "short":"true"
  },
  {  
     "title":"Priority",
     "value":"blocker",
     "short":"true"
    }
   ]
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Easy! Just use MergeContent and set the following configuration:

Merge Format: Binary Concatenation
Minimum Number of Entries: 2
Delimiter Strategy: Text
Header: [
Footer: ]
Demarcator: ,

(You could use MergeRecord but it is a little buggy for me at least).

Then transfer to JoltTrasnformJSON and set Jolt Transformation DSL to Shift and Jolt Specification to:

{
    "*": {
      "*": "&"
    }
}

This should do the job :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...