简体   繁体   中英

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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