如何使用JOLT将JSON转换为Flat?

时间:2019-01-02 09:45:53

标签: apache-nifi jolt

我只需要使attribute元素与id处于同一级别即可。

我只是有一个问题,无法将属性复制到同一级别。

这是我的示例JSON

{
  "data" : [ {
    "type" : "types",
    "id" : "CYY7",
    "attributes" : {
      "description" : null,
      "color" : "#dfc12d",
      "duration" : 15,
      "created_at" : "2017-01-10T04:51:22Z",
      "updated_at" : "2017-01-10T04:51:22Z",
      "slug" : "15min",
      "active" : false,
      "location" : null
    }
  }, {
    "type" : "types",
    "id" : "BGER",
    "attributes" : {
      "description" : null,
      "color" : "#8989fc",
      "duration" : 30,
      "created_at" : "2017-01-10T04:51:22Z",
      "updated_at" : "2017-01-10T04:51:22Z",
      "slug" : "30min",
      "active" : true,
      "location" : null
    }
  }

这是我的示例转换。

[{
    "operation": "shift",
    "spec": {
        "data": {
            "*": {
                "id": "event_type[&1].id",
                "type": "event_type[&1].type",
                "attributes": "event_type[&1].attributes[&1].description"
            }
        }
    }
}]

所需的输出将是

{
  "event_type" : [ {
    "type" : "types",
    "id" : "CYY7",
    "description" : null,
    "color" : "#dfc12d",
    "duration" : 15,
  }, {
    "type" : "types",
    "id" : "BGER",
    "description" : null,
    "color" : "#8989fc",
    "duration" : 30,
  }]
}

{ "event_type" : [ { "type" : "types", "id" : "CYY7", "description" : null, "color" : "#dfc12d", "duration" : 15, }, { "type" : "types", "id" : "BGER", "description" : null, "color" : "#8989fc", "duration" : 30, }] }

1 个答案:

答案 0 :(得分:4)

尝试使用此 Jolt Spec

[{
  "operation": "shift",
  "spec": {
    "data": {
      "*": {
        "id": "event_type[&1].id",
        "type": "event_type[&1].type",
        "attributes": {
          "description": "event_type[&2].description",
          "color": "event_type[&2].color",
          "duration": "event_type[&2].duration"
        }
      }
    }
  }
}]

输出:

{
  "event_type" : [ {
    "id" : "CYY7",
    "type" : "types",
    "description" : null,
    "color" : "#dfc12d",
    "duration" : 15
  }, {
    "id" : "BGER",
    "type" : "types",
    "description" : null,
    "color" : "#8989fc",
    "duration" : 30
  } ]
}