简体   繁体   English

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

[英]JoltTransformJSON processor in NiFi(json without any parent tag)

I need to transform a JSON data using the JoltTransformJSON inside Nifi, here's my spec which I am using for the transformation:我需要使用Nifi 中的 JoltTransformJSON转换 JSON 数据,这是我用于转换的规范:

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

My input data:我的输入数据:

[{
"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"
}
}]

Output what I m getting:输出我得到的:

{
"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"]
}

The required output:所需的输出:

[{
"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"
}
}]

Question: Can someone guide me where all I need to change my jolt spec to achieve the desired transformed output.问题:有人可以指导我在哪里更改我的震动规格以实现所需的转换输出。

The trick is to walk back up the tree until you get to the element in the array, then you can reference that index in the target.诀窍是返回树直到到达数组中的元素,然后您可以在目标中引用该索引。 Try this spec:试试这个规范:

[
  {
    "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"
        }
      }
    }
  }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM