Simple jolt transformation - transformation

I'm struggling with a very simple jolt transformation. I want to copy the whole input json (structure not known) into an array in the output json.
So for example the input could be:
{
"rating": {
"quality": {
"value": 3,
"max": 5
}
}
}
The output should look like this:
{
"items": [{
"item": {
"rating": {
"quality": {
"value": 3,
"max": 5
}
}
}
}]
}
Basically put the whole input json into an object called item into an array called items.
Could you please help me?
thx

I found the solution by myself. The required spec is
[
{
"operation": "shift",
"spec": {
"#1": {
"#": "items[].item"
}
}
}
]

[
{
"operation": "shift",
"spec": {
"#": "items[0].item"
}
}
]

Related

How to add a keyname to every element in an array in Nifi (Json)

I am quite new to Nifi and I already run into an issue:
I have a list of items in an array:
{
"locations": [
1,
2,
3
]
}
I want to transform this to:
{
"locations": [
{
"locationid": 1
},
{
"locationid": 2
},
{
"locationid": 3
}
]
}
Any ideas?
(After this I also want to add another element from a nifi attribute, but I think I will be able to manage that myself.)
Never mind:
[
{
"operation": "shift",
"spec": {
"locations": {
"*": {
"#": "[#2].locationID"
}
}
}
},
{
"operation": "modify-default-beta",
"spec": {
"*": {
"ReportTimestamp": "${ReportTimestamp:toDate('yyyy-MM-dd HH:mm:ss'):toNumber()}"
}
}
}
]

How to add a object to a Json in a Jolt Transform spec

In nifi, I am trying to transform a JSON with a variable amount of keys, but will always have a "date" key. I would like to transform the Json and change the string value of the date into a json object. However I am not getting what I need. Which operation/spec can I use in order to accomplish the expected output.
Input:
{
"name": "val1",
"date": "2021-05-19T00:53:20+00:00"
}
Spec:
[
{
"operation": "shift",
"spec": {
"#0": "wrapper"
}
}, {
"operation": "default",
"spec": {
"wrapper": {
"date": { "$date": "${date_attr}" }
}
}
}
]
expected output
{
"wrapper": {
"name": "val1",
"date": {"$date": "2021-05-19T00:53:20+00:00"}
}
}
what I am getting
{
"wrapper": {
"name": "val1",
"date": "2021-05-19T00:53:20+00:00"
}
}
Only single step of shift transformation along with the escaping characters (\\) for $ operator would suffice to use such as
[
{
"operation": "shift",
"spec": {
"name": "wrapper.name",
"date": { "#(1,date)": "wrapper.&.\\$date" }
}
}
]
where we're going one level up by using the first argument 1 in #(1,date) as been staying in the nested object
Edit : Considering that you only need to override the attribute date, and leave the others as they're without individually adding, use the following which again has only single step of shift transformation
[
{
"operation": "shift",
"spec": {
"*": "wrapper.&",
"date": { "#(1,date)": "wrapper.&.\\$date" }
}
}
]
In case of default, if the key mentioned in the spec is missing in the input json, then its added or else no changes may happen.
Here you are trying to push a node to a more level, which can be achieved by creating and assigning an temporary node.
\\ is an escape character.
[
{
"operation": "shift",
"spec": {
"date": "date1",
"#0": "."
}
},
{
"operation": "remove",
"spec": {
"date": ""
}
}, {
"operation": "shift",
"spec": {
"date1": "wrapper.date.\\$date",
"#0": "wrapper"
}
}, {
"operation": "remove",
"spec": {
"wrapper": {
"date1": ""
}
}
}
]

Extract multiple json fields using nifi

I'm new to Nifi. I have below json structure to be implemented using Nifi..
{
"jobs": [
{
"jid": "1",
"name": "job1",
"state": "FAILED",
"start-time": 12243
},
{
"jid": "2",
"name": "job2",
"state": "FAILED",
"start-time": 1233
},
{
"jid": "3",
"name": "job2",
"state": "RUNNING",
"start-time": 1223213
}
]
}
I need the output to be like below filtered only FAILED job details
{
[
job1,
job2
]
}
Please help and thanks in advance.
This spec will output a JSON array [job1,job2]:
[
{
"operation": "shift",
"spec": {
"jobs": {
"*": {
"state": {
"FAILED": {
"#(2,name)": "[]"
}
}
}
}
}
}
]
Your desired output of an array inside braces is not valid JSON, as objects need key/value pairs. If instead you want the array in an object, let's call the key failed_jobs and you can use the following spec:
[
{
"operation": "shift",
"spec": {
"jobs": {
"*": {
"state": {
"FAILED": {
"#(2,name)": "failed_jobs[]"
}
}
}
}
}
}
]

In Jolt How to select based on jsonarray value

In JOLT Based on the jsonarray values the output key and value should be added.
In the output CO2_VAL,CO_VAL,O3_VAL shoould come based on param value in env_values. So, how to apply the above filter.
The input payload is:
{
"id":"abcd",
"env_values":[
{
"param":"CO2",
"values":"20.0"
},
{
"param":"CO",
"values":"21.0"
},
{
"param":"O3",
"values":"22.0"
}
]
}
The output is :
{
"sl":"abcd",
"CO2_VAL":"20.0",
"CO_VAL":"21.0",
"O3_VAL":"22.0"
}
Hope it is what you ment. First of all, we are adding '_VAL' to the key. In the second spec we are putting keys to the values. And at the end we are pairing each key with value.
[
{
"operation": "shift",
"spec": {
"id": "s1",
"env_values": {
"*": {
"param": {
"*": "param[&2].t.&_VAL"
},
"values": "param[&1].values"
}
}
}
},
{
"operation": "shift",
"spec": {
"s1": "s1",
"param": {
"*": {
"t": {
"*": {
"$": "param[&3].key"
}
},
"values": "param[&1].value"
}
}
}
},
{
"operation": "shift",
"spec": {
"s1": "s1",
"param": {
"*": {
"value": "#(1,key)"
}
}
}
}
]

Nifi JoltTransformJSON : Keeping the underscore sign in key name

I am trying to write a jolt in Apache Nifi that converts the json form. I managed to convert the jolt but one attribute is missing underscore:
Output:
{
"Source": { //source is missing underscore. It should look like _Source
"userName": "Lulu"
}
}
My input is as following:
{
"user_name": "Lulu"
}
Currently my jolt expression looks like following:
[
{
"operation": "shift",
"spec": {
"user_name":"userName"
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"_Source":{
"userName":"#(2,userName)"
}
}
},
{ "operation": "remove",
"spec": {
"userName": "" }
}
]
How do I keep the underscore sign in attribute "Source"?
I am stuck at figuring out this part. I'm wondering what am I missing in the jolt expression. Thanks in advance, guys
I think the underscore might be some kind of special character in that operator, try double-backslashes in front of _Source:
[
{
"operation": "shift",
"spec": {
"user_name": "userName"
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"\\_Source": {
"userName": "#(2,userName)"
}
}
},
{
"operation": "remove",
"spec": {
"userName": ""
}
}
]
Can do it just with a single "shift".
Spec
[
{
"operation": "shift",
"spec": {
"user_name": "_Source.userName"
}
}
]

Resources