Convert two arrays into single array of objects - apache-nifi

I have two arrays and need to convert these arrays into a single object array using jolt.
Input
"Name": ["Test Test", "Test2 Test"]
"email": ["Test#tesasds.com", "Test2#test.com"]
output
[{"Name":"Test Test","email":"Test#tesasds.com"},{"Name":"Test2 Test","email":"Test2#test.com"}]

Description inline:
[
{
"operation": "shift",
"spec": {
// for every key
"*": {
// and every item in the array
"*": {
// create a structure <index of array> : key : value
// This will therefore join the two because the indexes will be the same
"#": "&.&2"
}
}
}
},
{
//Remove the index as key
"operation": "shift",
"spec": {
"*": "[]"
}
}
]
I'd suggest running the first shift to get understanding of the description.

Related

How to add new key in json flow by nifi jolt processor

how to convert the following data
{
"name" : "abcd",
"middleName" : "srivastav"
}
into
{
"name" : "abcd",
"middleName" : "srivastav"
"id" : "abcd"
}
by using a jolt transform such that the id and the name are within the same object as the expected output.
modify-default-beta or default operations will work for this :
[
{
"operation": "modify-default-beta",
"spec": {
"id": "abcd"
}
}
]
One option is to replicate the value of name through use of "#(0,name)": while keeping the current attributes by "*": "&" notation within a shift transformation such as
[
{
"operation": "shift",
"spec": {
"*": "&",
"#(0,name)": "id"
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
or another option is to use a modify(overwrite/default) transformation to dynamically replicate the value of name attribute for id such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"id": "#(1,name)"
}
}
]
the second demo is

Replace comma(,) with and in JOLT

Input:
{
"ratings":["1","2"]
}
I need to replace this comma to and using JOLT. Is this possible in JOLT??
Expected Output:
{
"ratings": "1 and 2"
}
Yes possible, you can use modify-overwrite-beta transformation along with string concatenation function join such as
[
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=join(' and ',#(1,&))"
}
}
]
or prefer only
[
{
"operation": "modify-overwrite-beta",
"spec": {
"ratings": "=join(' and ',#(1,&))"
}
}
]
for only one individual key-value pair(if there are more than one lists)
Edit : In the case, you have an array with unsorted elements, and converting to the desired concatenated string after sorting whenever presumingly you use it within Apache-Nifi, then add two JoltTransformJSON processor, perform the below operation within the first processor ;
[
{
"operation": "modify-overwrite-beta",
"spec": {
"ratings": "=sort(#(1,&))"
}
}
]
then apply one of the cases with the join function as the second step.

Jolt - Get values using a list of keys, alternatives to #(2,#)

I need to create a JSON array in order to split it into several jobs with Nifi.
The array needs to be created based on an existing array inside the JSON.
Can't figure out how to dynamically create a reference to another object in the JSON. I want the reference "#(2,#)" to work, but this is not supported.
INPUT
{
"name": "Loki",
"id": "1234",
"loc": "Utgard",
"age": "unknown",
"listitems": [
"name",
"id"
]
}
SPEC (that doesn't work):
[
{
"operation": "shift",
"spec": {
// Loop all listitems
"listitems": {
"*": {
// Get the value of the current item and push to processlist.type array
"#": "processlist[#2].type",
// Here is the problem, I need to get the "top level" value for the current value/key
"#(2,#)": "processlist[#2].value"
}
}
}
}
]
Expected output:
{
"processlist" : [
{
"type" : "name",
"value" : "Loki"
}, {
"type" : "id",
"value" : "1234"
}
]
}
SPEC (that will run but is not correct)
[
{
"operation": "shift",
"spec": {
// Loop all listitems
"listitems": {
"*": {
// Get the value of the current item and push to processlist.type array
"#": "processlist[#2].type",
// Here is the problem, I need to get the top level value for the current value/key
// Forcing this to "name" will at least execute the code
"#(2,name)": "processlist[#2].value"
}
}
}
}
]
Any ideas?
You can proceed one more step by adding "*" key to nest the current spec more while roaming by #(3,&) dynamically as this ampersand represents the incurred key values name and id such as
[
{
"operation": "shift",
"spec": {
"listitems": {
"*": {
"*": {
"#1": "processlist[#3].type",
"#(3,&)": "processlist[#3].value"
}
}
}
}
}
]

Apache Nifi - Split Array and format as JSON?

Probably a simple beginners question:
Using NIFI, I want to split an array (represented flowfile-content) in the form of
["x1","x2", ..]
and format it to a JSON object of the form
{"key1":"x1", "key2":"x2", ..}
(also as flowfile-content)
What processor is the most efficent to be used, how would the expression script look like ?
Thanks in advance,
Marc
Applying Jolt transformations through use of a JoltTransformJSON processor might be a good choice. In which, you can add such a specification below by clicking on the ADVANCED button that's stated in the SETTINGS tab of Configure Processor dialog box :
[
//Determine respective lists to be used to calculate their sizes within the next step
//in order to get ordinals for the key values
{
"operation": "shift",
"spec": {
"*": {
"*": "&(0,0).[#2]"
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=size(#(1,&))"
}
},
//exchange key and values
{
"operation": "shift",
"spec": {
"*": {
"$": "#(0)"
}
}
},
//add the desired word such as "key"
{
"operation": "shift",
"spec": {
"*": "key&(0,0)"
}
}
]

transform ExecuteSQL record in Apache Nifi

I am a newbie to Nifi so lack still in learning this too. I have a task to get data from executeSQL and the record return like this:
[{
"PKG_HA_01_ON":2.0,
"PKG_HA_03_ON":28.0,
"PKG_HA_04_ON":7.0,
"PKG_HA_05_ON":0.0,
"PKG_HA_06_ON":1.0,
"PKG_HA_09_ON":5.0
},
{
"PKG_HA_01_ON":8.02905,
"PKG_HA_03_ON":57.29038333333333,
"PKG_HA_04_ON":2.2858666666666663,
"PKG_HA_05_ON":60.0,
"PKG_HA_06_ON":12.291533333333332,
"PKG_HA_09_ON":12.3363
}]
This is the result of a union query. Now, I would like to convert into this:
[{
"machine":"PKG_HA_01_ON",
"counter":2.0,
"duration":8.02905
},
{
"machine":"PKG_HA_03_ON",
"counter":28.0,
"duration":57.29038333333333
}]
I have researched on JoltTransformJSON but still stuck with it. So what is the best way to achieve desired JSON?
Thanks in advance!!!
You can consecutively apply shift transformation as
[
{
"operation": "shift",
"spec": {
"*": {
"#PKG_HA_01_ON": "PKG_HA_01_ON.&",
"#PKG_HA_03_ON": "PKG_HA_03_ON.&"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"$": "&.machine",
"#(1,&.0)": "&.counter",
"#(1,&.1)": "&.duration"
}
}
},
{
"operation": "shift",
"spec": {
"*": ""
}
}
]
Restrict the result set to two keys(PKG_HA_01/3_ON) in the first step while determining the values for the keys 0 and 1 respectively, then convert those numbered keys to yours (counter and duration respectively) while adding the machine element, and remove the key names at the last step.

Resources