【问题标题】:Conversion of data text into JSON data将数据文本转换为 JSON 数据
【发布时间】:2022-07-13 17:55:16
【问题描述】:

如何通过nifi处理器进行转换

{
  "values": [
    [
      "id",
      "type",
      "name",
      "mobile no"
    ],
    [
      "xyz",
      "detail",
      "bob",
      "5283992123"
    ],
    [
      "pqr",
      "seconDEtail",
      "bob2",
      "6746789887"
    ]
  ]
}

进入

[
  {
    "id": "xyz",
    "type": "detail",
    "name": "bob",
    "mobile no ": "5283992123"
  },
  {
    "id": "pqr",
    "type": "seconDEtail",
    "name": "bob2",
    "mobile no ": "6746789887"
  }
]

如何通过 nifi 处理器以有效的方式进行这种转换。数据是由某个远程位置动态提供的

【问题讨论】:

  • 文本数据以逗号分隔

标签: json transform apache-nifi jolt


【解决方案1】:

您可以通过运行两班制获得所需的输出:

  1. 创建临时对象,并按数组中的位置对键和值进行分组。
  2. 然后迭代该对象值以创建输出
[
  {
    "operation": "shift",
    "spec": {
      "values": {
        // first element in array
        "0": {
          // for all items in array
          "*": {
            // set value as n.key
            "@": "&.key"
          }
        },
        // for everything else in the array
        "*": {
          // for all items in array
          "*": {
            // set values as n.value[]
            "@": "&.values"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      // for all items in our temp object
      "*": {
        "values": {
          // for each values
          "*": {
            // set value with key in our temp object and use index as array position
            "@": "[&1].@(3,key)"
          }
        }
      }
    }
  }
]

并更改特定键的值:

[
  {
    "operation": "shift",
    "spec": {
      "values": {
        // first element in array
        "0": {
          // for all items in array
          "*": {
            // match id
            "id": {
              // override value
              "#transaction_id": "&2.key"
            },
            // for everything else
            "*": {
              // set value as n.key
              "@1": "&2.key"
            }
          }
        },
        // for everything else in the array
        "*": {
          // for all items in array
          "*": {
            // set values as n.value[]
            "@": "&.values"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      // for all items in our temp object
      "*": {
        "values": {
          // for each values
          "*": {
            // set value with key in our temp object and use index as array position
            "@": "[&1].@(3,key)"
          }
        }
      }
    }
  }
]

【讨论】:

  • 我们能不能把“id”之类的属性改成“transaction_id”
  • 我添加了另一个转换,允许您更改键。
猜你喜欢
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-23
  • 2020-10-30
  • 2022-11-03
  • 2016-04-27
相关资源
最近更新 更多