How to add indices in an array in jolt transformation - transformation

I am trying to modify my input and add an id for each object with '-indexNum' but this seems not working.Here I m posting my code.
My input,
{
"employees": [{
"f_name": "tom",
"l_name": "smith"
},
{
"f_name": "don",
"l_name": "jones"
}
]
}
Expected o/p:
{
"employees": [{
"firstName": "tom",
"lastName": "smith",
"test": "emp-"
}, {
"firstName": "don",
"lastName": "jones",
"test": "emp-"
}]
}
My spec,
[{
"operation": "shift",
"spec": {
"employees": {
"*": {
"f_name": "employees[&1].firstName",
"l_name": "employees[&1].lastName"
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"employees": {
"*": {
"test": "=concat('emp-',)"
}
}
}
}]
I am trying to modify my input and add an id for each object with '-indexNum'.Can anyone please suggest me help.Thanks.

Spec
[
{
"operation": "shift",
"spec": {
"employees": {
"*": {
"$": "employees[&1].employeeNum",
"f_name": "employees[&1].firstName",
"l_name": "employees[&1].lastName"
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"employees": {
"*": {
"employeeNum": "=concat('emp-',#(1,employeeNum))"
}
}
}
}
]

Related

JOLT transformation for nested array

I had broken my mind with JOLT.
I need to group by data[].key then transform array in each group to object.
I had source JSON
{
"status": "OK",
"data": [
{
"value": 1,
"key": "John",
"subKey": "1W"
},
{
"value": 1,
"key": "John",
"subKey": "3W"
},
{
"value": 2,
"key": "John",
"subKey": "2W"
},
{
"value": 4,
"key": "John",
"subKey": "older"
},
{
"value": 5,
"key": "Tharanya",
"subKey": "2W"
},
{
"value": 5,
"key": "Tharanya",
"subKey": "1W"
}
]
}
Desired result is:
{
"status": "OK",
"data": [
{
"user": "John",
"1W": 1,
"2W": 2,
"3W": 1,
"older": 4
},
{
"user": "Tharanya",
"2W": 5,
"1W": 5
}
]
}
This transformation aggregates flat-list to map
[
{
"operation": "shift",
"spec": {
"data": {
"*": {
"#": "data.#(1,key)"
}
}
}
}
]
This one transform array to object
[
{
"operation": "shift",
"spec": {
"*": {
"value": "#(1,subKey)"
}
}
}
]
I need help to combine this transformation to chain.
Thank you.
IMAGE: Group transformation
IMAGE: array to object
You can mix up those current methods within the first shift transformation such as
[
{
"operation": "shift",
"spec": {
"*": "&",
"data": {
"*": {
"key": "&2.#(1,key).user",
"#value": "&2.#(1,key).#subKey"
}
}
}
},
{
// get rid of key names of the objects within the "data" array
"operation": "shift",
"spec": {
"*": "&",
"data": {
"*": "&1"
}
}
},
{
// get rid of repeating components of "user" attribute
"operation": "cardinality",
"spec": {
"*": {
"*": {
"*": "ONE"
}
}
}
}
]

Jolt transform to put the json content in a field using NiFi

My JSON Input
[
{
"id": "1234",
"Status": "null",
"Expired": "null",
"First_Name": "null"
},
{
"id": "5678",
"Status": "null",
"Expired": "null",
"First_Name": "null"
}
]
My JOLT Spec
[
{
"operation": "shift",
"spec": {
"*": {
"id": "[&1].id"
}
}
},
{
"operation": "default",
"spec": {
"*": {
"Status": "null",
"Expired": "null",
"First_Name": "null"
}
}
}
]
Output:
[
{
"id": "1234",
"First_Name": "null",
"Expired": "null",
"Status": "null"
},
{
"id": "5678",
"First_Name": "null",
"Expired": "null",
"Status": "null"
}
]
Expected output:
{
"isInput": true,
"input": [
{
"id": "1234",
"First_Name": "null",
"Expired": "null",
"Status": "null"
},
{
"id": "5678",
"First_Name": "null",
"Expired": "null",
"Status": "null"
}
]
}
Can this be done using NiFi JoltTransformJSON. Like put the json content in a field "input" and add another isInput key.
If not should I use replaceText with replacement value
{
"isInput": true,
"input": "${input}"
}
But, then how should I put output of JoltTransform to ${input} field
You could add the following two specs to your chain:
{
"operation": "shift",
"spec": {
"isInput": "isInput",
"*": "input[]"
}
},
{
"operation": "default",
"spec": {
"isInput": "true"
}
}
But it's shorter to just use this chain spec:
[
{
"operation": "shift",
"spec": {
"*": {
"id": "input[&1].id"
}
}
},
{
"operation": "default",
"spec": {
"input[]": {
"*": {
"Status": "null",
"Expired": "null",
"First_Name": "null"
}
},
"isInput": "true"
}
}
]
One step of shift step will suffice such as
[
{
"operation": "shift",
"spec": {
"#true": "isInput",
"#": "input"
}
}
]
where # sign key will copy all of the content for the current level.

Jolt transform convert large array to smaller array

My input JSON:
[
{
"label": [
{
"name": "abc"
}
]
},
{
"label": [
{
"name": "xyz"
}
]
}
]
My spec:
[{
"operation": "shift",
"spec": {
"*": {
"label": {
"0": {
"name": "name"
}
}
}
}
}]
Expected output:
[{
"name": "abc"
}, {
"name": "xyz"
}]
Generated output:
{
"name" : [ "abc", "xyz" ]
}
How do I not combine the array in the spec?
Try this spec,
[
{
"operation": "shift",
"spec": {
"*": {
"label": {
"*": {
"name": "[&3].name"
}
}
}
}
}
]

Jolt Transform to Array with Parent Attributes

I am using NiFi Jolt Processor to transform some JSON data.
My JSON field genre contains shared attributes and an array that contains the list of actual genre names.
I needed to transform the "genre" attribute to become an array of "genres" containing the list of the common attributes and the different genre names.
I have the following input JSON:
{
"programIdentifier": "9184663",
"programInstance": {
"genre": {
"source": "GN",
"locked": false,
"lastModifiedDate": 1527505462094,
"lastModifiedBy": "Some Service",
"genres": [
"Miniseries",
"Drama"
]
}
}
}
I have tried the following spec:
[{
"operation": "shift",
"spec": {
"programIdentifier": ".&",,
"genre": {
"source": "genres[].source.value",
"locked": "genres[].locked",
"lastModifiedDate": "genres[].lastModifiedDate",
"lastModifiedBy": "genres[].lastModifiedBy",
"genres": {
"*": "genres[&0].name"
}
}
}]
This my expected output:
{
"programIdentifier": "9184663",
"programInstance": {
"genres": [
{
"source": {
value: "GN"
}
"locked": false,
"lastModifiedDate": 1527505462094,
"lastModifiedBy": "Some Service",
"name": "Miniseries"
},
{
"source": {
value: "GN"
}
"locked": false,
"lastModifiedDate": 1527505462094,
"lastModifiedBy": "Some Service",
"name": "Drama"
}
]
}
}
But it's coming out as:
{
"programIdentifier": "9184663",
"programInstance": {
"genres": [
{
"source": {
"value": "GN"
},
"name": "Miniseries"
}, {
"locked": false,
"name": "Drama"
}, {
"lastModifiedDate": 1527505462094
}, {
"lastModifiedBy": "Some Service"
}],
}
}
Is it what you want to achieve?
[
{
"operation": "shift",
"spec": {
"programIdentifier": ".&",
"programInstance": {
"genre": {
"genres": {
"*": {
"#2": {
"source": "programInstance.genres[&2].source[]",
"locked": "programInstance.genres[&2].locked",
"lastModifiedDate": "programInstance.genres[&2].lastModifiedDate",
"lastModifiedBy": "programInstance.genres[&2].lastModifiedBy"
},
"#": "programInstance.genres[&1].name"
}
}
}
}
}
}
]

I need to transform below input to some out put using JOLT Transformation

I need to transform below input to exact output using JOLT transformation.
Input:
{
"First_Name": "Test",
"Last_Name": "User",
"allpointofInterest": "someText",
"campaign_id": "123456789",
"lead_id": "123456789"
}
Output:
"input" : {
"tokens" : [ {
"name" : "{{my.First_Name}}",
"value" : "Test"
}, {
"name" : "{{my.Last_Name}}",
"value" : "User"
}, {
"name" : "{{my.allpointofInterest}}",
"value" : "someText"
} ]
},
"leads": [{
"id": "123456789"
}]
}
I tried with below spec but not driven to exact output what i needed.
spec:
[
{
"operation": "remove",
"spec": {
"campaign_id": ""
}
}, {
"operation": "shift",
"spec": {
"lead_id": {
"#": "input.leads[#1].id"
},
"*": "temptoken.&"
}
}, {
"operation": "shift",
"spec": {
"*": "&",
"temptoken": {
"*": {
"$": "input.tokens[#2].tmpname",
"#": "input.tokens[#2].value"
}
}
}
}, {
"operation": "modify-overwrite-beta",
"spec": {
"input": {
"leads": {
"*": {
"id": ["=toInteger", 0]
}
}
}
}
}, {
"operation": "modify-default-beta",
"spec": {
"input": {
"tokens": {
"*": {
"name": "=concat('{{my.',#(1,tmpname), '}}')"
}
}
}
}
}, {
"operation": "remove",
"spec": {
"input": {
"tokens": {
"*": {
"tmpname": ""
}
}
}
}
}
]
Any help would be much appreciated. Thanks
Does sth like that suits you?
[
{
"operation": "shift",
"spec": {
"#{{my.First_Name}}": "input.tokens[0].name",
"First_Name": "input.tokens[0].value",
"#{{my.Last_Name}}": "input.tokens[1].name",
"Last_Name": "input.tokens[1].value",
"#{{my.allpointofInterest}}": "input.tokens[2].name",
"allpointofInterest": "input.tokens[2].value",
"lead_id": "leads[#].id"
}
}
]

Resources