How to recursively iterate over a tag using jolt - transformation

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"
}
}
}
}
}
}
]

Related

NiFi Jolt Specification for array input

I have the following input in Nifi Jolt Specification processor:
[
{
"values": [
{
"id": "paramA",
"value": 1
}
]
},
{
"values": [
{
"id": "paramB",
"value": 3
}
]
}
]
Expected output:
[
{
"id": "paramA",
"value": 1
},
{
"id": "paramB",
"value": 2
}
]
Can you explain how I have to do?
thanks in advance
You want to reach the objects of the values array which are nested within seperate object signs ({}). A "*" notation is needed in order to cross them over per each individual values array, and then use another "*" notation for indexes of those arrays while choosing "" as the counterpart values in order to grab nothing but the sub-objects such as
[
{
"operation": "shift",
"spec": {
"*": {
"values": {
"*": ""
}
}
}
}
]

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"
}
}
}
}
}
]

Convert two arrays into single array of objects

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.

Jolt Spec for moving the json objects into the simple array

I am having an array of objects in the given format:
[
{
"meta": [
{
"id": "101A"
},
{
"id": "101B"
}
]
}
]
Can someone help me with jolt spec I want the final output in the following format:
[
{
"meta": [
"101A",
"101B",
......
]
}
]
Thanks in advance!
Basically you want to iterate over your two arrays using the "*", and when iterating over your second array (meta) just get the id attribute and send to the meta array (ignore the object). See if this helps you understand:
[
{
"operation": "shift",
"spec": {
"*": {
"meta": {
"*": {
"id": "meta"
}
}
}
}
}
]

Resources