IF condition with logical operators in JOLT mapping - jolt

Is there a way to use if condition with AND / OR in JOLT?
I have a json where i need to check two field values and then set the mapping.
E.g
**Input:**
{
"oldStatus":"NEW",
"newStatus":"OPEN"
}
The JOLT logic should be doing something like this:
if(oldStatus == "NEW" AND newStatus=="OPEN")
then
status = "In Progress"
**Expected Output**
{
"status":"In Progress"
}

Try this spec,
[
{
"operation": "modify-default-beta",
"spec": {
"fullStat": "=concat(#(1,oldStatus),#(1,newStatus))"
}
},
{
"operation": "shift",
"spec": {
"fullStat": {
"NEWOPEN": {
"#In Progress": "status"
},
"*": {
"#Not In Progress": "status"
}
}
}
}
]

Related

Jolt transformation to retrieve key as value

I have this JSON input that I would like to transform via jolt spec
{
"message": {
"trx": {
"trxId": "1234"
},
"translation": {
"transactions": {
"1234": "http://www.trythisjolt.com"
}
}
}
}
expected output would be something like this
{
"message": {
"trx": {
"trxId": "1234",
"trxName": "http://www.trythisjolt.com"
}
}
}
Is this achievable with Jolt please? I have tried several possibilities but I'm very new to Jolt and have not managed so far
Yes achievable, you can use
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"trxId": "message.trx.&",
"transactions": { "*": "message.trx.trxName" }
}
}
}
}
]
where go to the innermost elements firstly, then prepend message.trx. for both while renaming the second one to the desired value. This way, both of them are nested within the common object.

Newbie: Transform json repetitions to Single Arrays and add Entry Inside

I'm looking for some Jolt help. I'm officially a newbie attempting to understand if Jolt will suffice for usage in our solution. I can't seem to get the following output as I would expect.
I was wondering if anyone could help me, and see if this template would be able to help out on our other needs for structures of the same fashion. Also, if anyone has the best/better reading they suggest on Jolt, I'm happy to hear of that as well.
In a nutshell, I'm trying to use JOLT to handle the following Needs:
From a JSON input, if there are multiple repetitions of a structure, output these repetitions as an array of the independent json structures.
From the parent level, take a named match SetIDAL1 and place into the top created json structure, but not as part of the array, and as its own element.
Here is an example of my input I need to transform:
"AL1": {
"0": {
"AllergenCodeMnemonicDescription": {
"1": {
"Text": "ASPIRIN",
"ID": "TEST1"
},
"2": {
"Text": "TYLENOL",
"ID": "TEST2"
}
},
"SetIDAL1": "1"
},
"1": {
"AllergenCodeMnemonicDescription": {
"1": {
"Text": "ADVIL"
}
},
"SetIDAL1": "2"
}
}
}
My current spec I'm using seems to get me pretty close. However, I can't get the arrays up a level in order to remove the 0:1, nor fix the fact that I want the SetIDAL1 value placed inside the newly created array object, instead it makes it's own array object. I've played around with various other options that only lead me further away. Any help for the solution and input/guidance, general "smart ways" to look at this issue would be appreciated.
Unfortunately, I do not have a copy of previous work I tried, which would perform the matching on all groups and map them as expected. I started moving toward matching each individual 0/1 object underneath my input in attempts to see if I could "bury" the SetIDAL properly, to no avail. I really do not want to code for each level, but hoping there's a solution for the "problem at hand" that someone can assist me with.
[
{
"operation": "shift",
"spec": {
"AL1": {
"0": {
"AllergenCodeMnemonicDescription": {
"#": "AL1.[].AllergenCodeMnemonicDescription.[]"
},
"SetIDAL1": "AL1.[].SetIDAL1"
},
"1": {
"AllergenCodeMnemonicDescription": {
"*": "AL1.[].AllergenCodeMnemonicDescription.[]"
},
"SetIDAL1": "AL1.[].SetIDAL1"
}
}
}
}
]
here is the output I am getting. I assume potentially I need yet another shift after this, to bring the "1"/"2" levels "UP again" somehow. But I can't seem to get the SetIDAL1 in the correct place as stated before.
{
"AL1" : [ {
"AllergenCodeMnemonicDescription" : [ {
"1" : {
"Text" : "ASPIRIN",
"ID" : "TEST1"
},
"2" : {
"Text" : "TYLENOL",
"ID" : "TEST2"
}
} ]
}, {
"SetIDAL1" : "1"
}, {
"AllergenCodeMnemonicDescription" : [ {
"Text" : "ADVIL"
} ]
}, {
"SetIDAL1" : "2"
} ]
}
Here is the output I need:
{
"AL1": [
{
"AllergenCodeMnemonicDescription": [
{
"Text": "ASPIRIN",
"ID": "TEST1"
},
{
"Text": "TYLENOL",
"ID": "TEST2"
}
],
"SetIDAL1": "1"
},
{
"AllergenCodeMnemonicDescription": [
{
"Text": "ADVIL"
}
],
"SetIDAL1": "2"
}
]
}
As the indexes start at 1 (not 0) was getting a null on the first element from each Allergen array and had to remove it in the end (not sure if theres a better way to do this), but this specs will do the trick:
[
{
"operation": "shift",
"spec": {
"AL1": {
"*": {
"AllergenCodeMnemonicDescription": {
"*": {
"Text": "AL1.[&3].AllergenCodeMnemonicDescription.[&1].&",
"ID": "AL1.[&3].AllergenCodeMnemonicDescription.[&1].&"
}
},
"SetIDAL1": "AL1.[&1].&"
}
}
}
},
{
"operation": "remove",
"spec": {
"AL1": {
"*": {
"AllergenCodeMnemonicDescription": {
"0": ""
}
}
}
}
}
]

How to perform divide operation in jolt using 'modify-overwrite-beta'

Input,
{"scores": [ 4,2,8,7,5 ] }
Output,
{"FirstElement": 2 } //This is generated by dividing the first element of the array by 2.
Spec,
[{
"operation": "modify-overwrite-beta",
"spec": {
"Avg": "=divide(=firstElement(#(1,scores)),2)"
}
}]
From the above Spec,I am trying to divide the first element of the list by 2 but the output I get is same as the input.
Can't nest "functions" with modify. Have to break it up into two steps.
[
{
"operation": "modify-overwrite-beta",
"spec": {
"firstElement": "=firstElement(#(1,scores))"
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"Avg": "=divide(#(1,firstElement),2)"
}
}
]

using jolt to transform empty field

my json looks like this
{
"group":{
"personnel":[
{
"member":{
"id":"1",
"name":"John"
}
},
{
"member":{
"id":"2",
"name":"Doe"
}
}
]
}
}
and the expected output for this is
[
{
"id":"1",
"name":"John"
},
{
"id":"2",
"name":"Doe"
}
]
but, there's also some time when the json is empty like so:
{}
for this, I want the output to be
[]
my spec looks like this
"spec":{
"group":{
"personnel":{
"*":{
"*":"[]"
}
}
}
}
but this doesnt work on the second case where json is empty, it'll just return null. do I need to add anything?
A tad clunky but this works.
Spec
[
{
"operation": "shift",
"spec": {
"group": {
"personnel": {
"*": {
// write to a temp array so that we can
// default it into existence later if needed
"*": "temp[]"
}
}
}
}
},
{
"operation": "default",
"spec": {
// If "temp" does not exist, make it be an empty array
"temp": []
}
},
{
"operation": "shift",
"spec": {
// write value at "temp" to the root level
"temp": ""
}
}
]

JSON to JSON changing array to one long string using JOLT

Lets say my json is:
{
"rating": {
"primary": {
"value": ["a","B",1]
}
}
}
What I want to achieve is:
{
"values": "a, B, 1"
}
I'm using jolt json to json code.
It is not possible with stock transformations of Jolt. You need to write your custom transformation. Check the documentation section on how to write your own transformations.
you can try the following spec
[
{
"operation": "modify-overwrite-beta",
"spec": {
"value": "=concat(#(2,rating.primary.value[0]),',',#(2,rating.primary.value[1]),',',#(2,rating.primary.value[2]))"
}
},
{
"operation": "remove",
"spec": {
"rating": ""
}
}
]