NiFi中的JoltTransformJSON处理器(没有任何父标签的json)

时间:2017-04-18 07:46:49

标签: apache-nifi jolt

我需要使用Nifi中的 JoltTransformJSON 转换JSON数据,这是我用于转换的规范:

[{
"operation": "shift",
"spec": {
    "*": {
        "Header": {
            "readOn": "created_date_time",
            "fileName": "readFile"
        },
        "Data": {
            "id": "Id",
            "first_name": "First_Name",
            "last_name": "Last_Name",
        }
    }
}}]

我的输入数据:

[{
"Header": {
    "readOn": "2017/04/18 10:55:05",
    "fileName": "sample1.csv",
    "recordNum": 1
},
"Data": {
    "last_name": "Martin",
    "id": 21,
    "first_name": "Clarence"
}
}, {
"Header": {
    "readOn": "2017/04/18 10:55:05",
    "fileName": "sample.csv",
    "recordNum": 2
},
"Data": {
    "last_name": "Graham",
    "id": 22,
    "first_name": "Walter"
}
}]

输出我得到的内容:

{
"created_date_time": ["2017/04/18 10:55:05", "2017/04/18 10:55:05"],
"readFile": ["sample1.csv", "sample2.csv"],
"Id": [21, 22],
"First_Name": ["Clarence", "Walter"],
"Last_Name": ["Martin", "Graham"]
}

所需的输出:

[{
"recordNum": 1,
"Header": {
    "created_date_time": "2017/04/18 10:55:05",
    "readFile": "getusroi.csv"
},
"Data": {
    "Last_Name": "Martin",
    "Id": 21,
    "First_Name": "Clarence"
}
}, {
"recordNum": 2,
"Header": {
    "created_date_time": "2017/04/18 10:55:05",
    "readFile": "getusroi.csv"
},
"Data": {
    "Last_Name": "Graham",
    "Id": 22,
    "First_Name": "Walter"
}
}]

问题:有人可以指导我改变我的震动规范以获得所需的转换输出。

1 个答案:

答案 0 :(得分:3)

诀窍是向上走回树直到你到达数组中的元素,然后你可以在目标中引用该索引。试试这个规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Header": {
          "recordNum": "[&2].recordNum",
          "readOn": "[&2].&1.created_date_time",
          "fileName": "[&2].&1.readFile"
        },
        "Data": {
          "id": "[&2].&1.Id",
          "first_name": "[&2].&1.First_Name",
          "last_name": "[&2].&1.Last_Name"
        }
      }
    }
  }
]