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

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

Related

How to read nested value using shift operation in JOLT

May be this is very basic but I am new to JOLT and I am not getting the expected output after several attempts.
I am trying to read a nested value using shift operation.
Basically the value of person is dynamic and based on this value, I need to read the value from profession object.
The input I am working on is huge and everything is working as expected, so I am just putting the part where I am stuck.
Input:
{
"person": "Tom Hanks",
"profession": {
"Tom Hanks": "actor",
"Christopher Nolan": "director"
}
}
Expected output:
{
"output" : "actor"
}
I am trying to achieve this using something like below. I know this is not correct format but just putting it to know if there is something like this which can be used to get the expected output.
[
{
"operation": "shift",
"spec": {
"#(1,profession).#(1,person)": "output"
}
}
]
Can someone please help.
Thanks in advance !!
I transformed the value as a key to match it with & operator. Maybe a more elegant way exists.
[
{
"operation": "shift",
"spec": {
"profession": "#(1,person)"
}
},
{
"operation": "shift",
"spec": {
"*": {
"&": "output"
}
}
}
]

Ignoring implicit Array creation in the output JSON

So currently if the input is
{
"foo": "bar",
"tuna": "marlin"
}
The spec is
[
{
"operation": "shift",
"spec": {
"foo": "baz",
"tuna": "baz"
}
}
]
The output is
{
"baz" : [ "bar", "marlin" ]
}
How do I make jolt not to create an array, but instead override the values of the "baz" key with the last element of the array?
Expected output
{
"baz" : "marlin"
}
As far as I understand, you only want to get the last element of the array. So, add one more spec to yours of operation type modify-overwrite-beta along with lastElement() function such as
[
{
"operation": "shift",
"spec": {
"*": "baz"
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=lastElement(#(1,&))"
}
}
]
in which no need to explicitly write each key within the shift spec, rather replace them with an asterisk. Then, the asterisk within the second spec also represents all of the keys(in this case, this is only baz), and ampersand next to that copies the name of the key in order to represent the relative value.

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.

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