【问题标题】:I have two Json payload. I want to merge them in a single Json object我有两个 Json 有效载荷。我想将它们合并到一个 Json 对象中
【发布时间】:2019-03-12 09:45:33
【问题描述】:

我有两个有效负载并希望将它们合并为单个 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"
    }
   ]
  }

【问题讨论】:

    标签: apache-nifi


    【解决方案1】:

    简单!只需使用MergeContent 并设置以下配置:

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

    (你可以使用MergeRecord,但至少对我来说有点麻烦)。

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

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

    这应该可以完成工作:)

    【讨论】:

    • MergeContent 不会提供所需的 JSON,它会将每个 JSON 作为数组的元素,但所需的 JSON 显示第二个 JSON 中的“字段”元素作为字段添加到第一个 JSON ......在我看来,这是一个不同于数据合并的数据连接
    • @BryanBende 这就是为什么我告诉他把它转移到JoltTrasnformJSON。第一步是合并 FlowFile 以作用于两个 JSON 对象,第二步是使用 JoltTransformJSON 将它们提取到一个对象中。
    • 我明白了,我很抱歉 :) 我只是快速阅读了答案的第二部分
    • @BryanBende 当然,如果您同意我的回答,请将其标记为有用:)
    • 当然,如果是,请选择我的答案(:
    【解决方案2】:

    一般来说,NiFi 并不意味着进行传统的流式连接,但邮件列表上的这个最近的帖子可以帮助解释什么是可能的:

    http://apache-nifi-users-list.2361937.n4.nabble.com/join-two-datasets-td7039.html

    【讨论】:

    • 它不适合复杂的连接。但它非常适合像这样简单的合并:)
    猜你喜欢
    • 2014-11-10
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    相关资源
    最近更新 更多