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.
Related
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"
}
}
}
}
}
]
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)"
}
}
]
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.
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.
I need to transform JSON using jolt. Input JSON is as follows:
{
"items": [
{
"Group1": {
"ABCCode": "3",
"ABCDescription": "abcd"
},
"Group2": {
"test2": [
"123"
]
}
}
]
}
and I need the output as the following,
[
{
"test2Id": "123",
"attrname": "ABCCode",
"attrval": "3"
},
{
"test2Id": "123",
"attrname": "ABCDescription",
"attrval": "abcd"
}
]
How can I achieve this using jolt?
This produces the output from the given input. You will probably need to tweak it, as you give it different types of input.
[
{
"operation": "shift",
"spec": {
"items": {
"*": {
"Group1": {
// match all keys below Group1
"*": {
// "$" means grab the key matched above,
// and write it the the output as "attrname"
"$": "[#2].attrname",
// "#" means grab the value matched above
"#": "[#2].attrval",
// walk back up the match tree three levels
// walk back down the path "Group2.test2[0]"
// and write that value to the output
"#(2,Group2.test2[0])": "[#2].test2Id"
}
}
}
}
}
}
]