问题
How to apply transformation to the json file having records in the following format (not array, just multiple objects). I want to provide a file with following input format and after applying transfornation.Want to get it saved in some folder. example:
Input Record Format
{
"name": "adam",
"age": 12,
"city": "australia"
}
{
"name": "adam",
"age": 12,
"city": "australia"
}
{
"name": "adam",
"age": 12,
"city": "australia"
}
{
"name": "adam",
"age": 12,
"city": "australia"
}
{
"name": "adam",
"age": 12,
"city": "australia"
}
{
"name": "adam",
"age": 12,
"city": "australia"
}
.....
Desired Output Record Format
{
"fname": "adam",
"age": 12,
"city": "australia"
}
{
"fname": "adam",
"age": 12,
"city": "australia"
}
{
"fname": "adam",
"age": 12,
"city": "australia"
}
{
"fname": "adam",
"age": 12,
"city": "australia"
}
{
"fname": "adam",
"age": 12,
"city": "australia"
}
{
"fname": "adam",
"age": 12,
"city": "australia"
}
// Just changing the name key to fname
.....
回答1:
Since you tagged apache-nifi
I assume you want to use one of the JOLT processors in Apache NiFi for this. In this case JoltTransformJSON won't work because it expects valid JSON as daggett said. However the JSON parser in JsonTreeReader is more lenient, so you can use JoltTransformRecord with a JsonTreeReader and the following spec:
[
{
"operation": "shift",
"spec": {
"name": "fname",
"*": "&"
}
}
]
Although you can't "pretty print" the JSON without it being valid JSON, you can set the Output Grouping property on the JsonRecordSetWriter to One Line Per Object
. That will give you input something like:
{"fname":"adam","age":12,"city":"australia"}
{"fname":"adam","age":12,"city":"australia"}
{"fname":"adam","age":12,"city":"australia"}
{"fname":"adam","age":12,"city":"australia"}
{"fname":"adam","age":12,"city":"australia"}
{"fname":"adam","age":12,"city":"australia"}
来源:https://stackoverflow.com/questions/61465241/how-to-provide-input-to-jolt-when-multiple-objects-are-given