I’m trying convert JSON by NiFi JOLTTransformRecord processor to tree with nested array.
But I am having some trouble with converting the flat JSON to Nested JSON. I have looked at examples and didn’t get any closer as to what is mentioned above. I need to transform a JSON structure by using a JOLT spec.
What I’m doing wrong?
Help Me please/
Source JSON
[
{
"log_date": "2024-07-24 16:10:22.851",
"log_level": "INFO",
"log_message": "Message1",
"request_id": "0a3d546d"
},
{
"log_date": "2024-07-24 16:10:22.851",
"log_level": "INFO",
"log_message": "Message2",
"request_id": "0a3d546d"
},
{
"log_date": "2024-07-24 16:10:22.851",
"log_level": "INFO",
"log_message": "Message3",
"request_id": "0a3d546d"
},
{
"log_date": "2024-07-24 16:10:22.572",
"log_level": "INFO",
"log_message": "Message4",
"request_id": "0a3d546d"
},
{
"log_date": "2024-07-24 16:10:22.572",
"log_level": "INFO",
"log_message": "Message5",
"request_id": "0a3d546d"
},
{
"log_date": "2024-07-24 16:10:22.572",
"log_level": "INFO",
"log_message": "Message6",
"request_id": "0a3d546d"
}
]
DESIRABLE output
{
"request_id": "0a3d546d",
"log": [
{
"log_date": "2024-07-24 16:10:22.851",
"log_level": "INFO",
"log_message": "Message1"
},
{
"log_date": "2024-07-24 16:10:22.851",
"log_level": "INFO",
"log_message": "Message2"
},
{
"log_date": "2024-07-24 16:10:22.851",
"log_level": "INFO",
"log_message": "Message3"
},
{
"log_date": "2024-07-24 16:10:22.572",
"log_level": "INFO",
"log_message": "Message4"
},
{
"log_date": "2024-07-24 16:10:22.572",
"log_level": "INFO",
"log_message": "Message5"
},
{
"log_date": "2024-07-24 16:10:22.572",
"log_level": "INFO",
"log_message": "Message6"
}
]
}
I’m trying use this specification
JOLT Specification
[
{
"operation": "shift",
"spec": {
"*": {
"request_id": "request_id",
"log*": {
"@(1)": "log"
}
}
}
},
{
"operation": "cardinality",
"spec": {
"request_id": "ONE"
}
},
{
"operation": "cardinality",
"spec": {
"logs": {
"log_date;log_level;log_message": "ONE"
}
}
}
]
2
Answers
You can make it online here.
You can use the Java library from here.
Also if only the given above JOLT specification would be used, you can write code using only the JSON library (Python json package, for example).
You might prefer using a single shift transformation spec in which the indexes of the array are matched for the ones with zero vs. others such as