我有两个Json有效负载。我想将它们合并到单个Json对象中

时间:2019-03-12 09:45:33

标签: apache-nifi

我有两个有效负载,想将它们合并到单个JSON对象中(流连接)。人们建议在少数地方使用AttributesToJSON,但是由于其中一个JSON没有固定的属性集,我想那是不可能的。

第一个有效载荷是

{  
   "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"
  }
 ]
}

我希望输出为

{   
"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"
    }
   ]
  }

1 个答案:

答案 0 :(得分:2)

容易!只需使用MergeContent并设置以下配置即可:

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

(您可以使用MergeRecord,但至少对我来说是个小问题)。

然后转移到JoltTrasnformJSON,并将Jolt Transformation DSL设置为Shift,将Jolt Specification设置为:

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

这应该可以完成:)