【问题标题】:Splitting Json to multiple jsons in NIFI在 NIFI 中将 Json 拆分为多个 json
【发布时间】:2019-12-17 22:19:26
【问题描述】:

我有下面的 json 文件,我想在 NIFI 中拆分它

输入:

    [ {
  "id" : 123,
  "ticket_id" : 345,
  "events" : [ {
    "id" : 3322,
    "type" : "xyz"
  }, {
    "id" : 6675,
    "type" : "abc",
    "value" : "sample value",
    "field_name" : "subject"
  }, {
    "id" : 9988,
    "type" : "abc",
    "value" : [ "text_file", "json_file" ],
    "field_name" : "tags"
  }]
  }]

我的输出应该是 3 个不同的 json,如下所示:

{
  "id" : 123,
  "ticket_id" : 345,
  "events.id" :3322,
  "events.type":xyz
  }

  {
  "id" : 123,
  "ticket_id" : 345,
  "events.id" :6675,
  "events.type":"abc",
  "events.value": "sample value"
  "events.field_name":"subject"
  }

  {
  "id" : 123,
  "ticket_id" : 345,
  "events.id" :9988,
  "events.type":"abc",
  "events.value": "[ "text_file", "json_file" ]"
  "events.field_name":"tags"

  }

我想知道我们可以使用 splitjson 吗?我的意思是 splitjson 可以根据 json 中存在的 json 对象数组拆分 json 吗?

如果有办法实现这一点,请告诉我。

【问题讨论】:

    标签: json apache-nifi


    【解决方案1】:

    如果您想要 3 个不同的流文件,每个文件都包含数组中的一个 JSON 对象,您应该能够使用 SplitJson 使用 $ 和/或 $.* 的 JSONPath 来完成此操作

    【讨论】:

    • 我是否需要一个接一个地有 2 个带有 $ 和 $.* 的 SplitJson?因为使用一个 $.* splitjson 和下一个 $.events 我得到的事件与 id 和 author_id 分开
    【解决方案2】:

    使用reduce函数:

    function split(json) {
        return json.reduce((acc, item) => {
            const events = item.events.map((evt) => {
                const obj = {id: item.id, ticket_id: item.ticket_id};
                for (const k in evt) {
                    obj[`events.${k}`] = evt[k];
                }
                return obj;
            });
            return [...acc, ...events];
        }, []);
    }
    
    const input = [{"id":123,"ticket_id":345,"events":[{"id":3322,"type":"xyz"},{"id":6675,"type":"abc","value":"sample value","field_name":"subject"},{"id":9988,"type":"abc","value":["text_file","json_file"],"field_name":"tags"}]}];
    const res = split(input);
    console.log(res);
    

    【讨论】:

    • 谢谢你,但是我想知道我们是否可以使用 Nifi 中的处理器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 2018-12-18
    相关资源
    最近更新 更多