Transform json by filtering array of objects and show single element - jolt

I am trying to transform using JOLT.
Below are my input, expected and spec. How do I show only currency without curly brackets?
Input
{
"txn": [
{
"direction": "BUY",
"currency": "USD"
},
{
"direction": "SELL",
"currency": "EUR"
}
]
}
Specification
[
{
"operation": "shift",
"spec": {
"txn": {
"*": {
"direction": {
"BUY": {
"#2": "buyCurrency"
}
}
}
}
}
}
]
Expected output
{
"buyCurrency" : "USD"
}
Current output
{
"buyCurrency" : {
"direction" : "BUY",
"currency" : "USD"
}
}

Try
[
{
"operation": "shift",
"spec": {
"txn": {
"*": {
"direction": {
"BUY": {
"#2": "."
}
}
}
}
}
},
{
"operation": "remove",
"spec": {
"direction": ""
}
},
{
"operation": "shift",
"spec": {
"currency": "currency"
}
}
]

Related

Facing issue with JOLT transformation with nested array

I have a JSON input :
{
"id": "Root_ID",
"Item": [
{
"id": "ID_1",
"characteristic": [
{
"name": "char1",
"value": "PRE1"
},
{
"name": "char2",
"value": "2050-01-01"
}
]
},
{
"id": "ID_2",
"characteristic": [
{
"name": "char1",
"value": "PRE2"
},
{
"name": "char2",
"value": "2050-01-02"
}
]
}
]
}
which needs to be converted by using a Jolt transformation spec to the following output :
{
"id": "Root_ID",
"Item": [
{
"id": "ID_1",
"char1": "PRE1",
"char2": "2050-01-01"
},
{
"id": "ID_2",
"char1": "PRE2",
"char2": "2050-01-02"
}
]
}
Currently, I'm using this spec :
[
{
"operation": "shift",
"spec": {
"id": "id",
"Item": {
"*": {
"characteristic": {
"*": {
"name": {
"char1": {
"#(2,value)": "item[#3].char1"
},
"char2": {
"#(2,value)": "item[#3].char2"
}
}
}
}
}
}
}
}
]
which does not produce the desired result.
Can you please help me prepare a correct spec to handle this issue ?
Edit : What if I'd like to get the following JSON result ?
{
"id": "Root_ID",
"Item": [
{
"id": "ID_1",
"char1": "PRE1"
},
{
"id": "ID_2",
"char1": "PRE2",
"char2": "2050-01-02"
}
]
}
You can use the following shift transformation spec in which #value and #name are matched reciprocally such as
[
{
"operation": "shift",
"spec": {
"id": "&",
"Item": {
"*": {
"id": "&2[&1].&",// you can reduce two levels based on the inner identifier 4,3 -> 2,1
"char*": {
"*": {
"#value": "&4[&3].#name" // &4 copies the literal "Item" after going four levels up the tree, [&3] generates array-wise result(array of objects) after going three levels up the tree to reach the level of the indexes of the "Item" array
}
}
}
}
}
}
]
Edit : You can alternatively use the following spec for the case in which you need to return char2 within the all objects but the first :
[
{
"operation": "shift",
"spec": {
"id": "&",
"Item": {
"0": {// stands for the first index
"id": "&2[&1].&",
"char*": {
"*": {
"name": {
"char1": {
"#2,value": "&6[&5].#3,name"
}
}
}
}
},
"*": {
"id": "&2[&1].&",
"char*": {
"*": {
"#value": "&4[&3].#name"
}
}
}
}
}
}
]

JOLT Merging dissimilar elements

Here is my input
[{
"name": "LAST_UPDATE_DATE",
"value": 1075456461000
},
{
"name": "LAST_UPDATED_BY",
"value": {
"value": "BCw="
}
}]
Here is the expected output
[{
"name": "LAST_UPDATE_DATE",
"value": 1075456461000
},
{
"name": "LAST_UPDATED_BY",
"value": "BCw="
}]
Tried to add default fields so that I can compare further, but can't seem to create a node with a value already existing ( value: 1075456461000 )
[{
"operation": "modify-default-beta",
"spec": {
"*": {
"val" : null,
"value": {
"val" : null
}
}
}
}]
Any suggestions are appreciated.
After several hours of struggle here is what I came up with ....
[
{
"operation": "shift",
"spec": {
"*": {
"$": "&1.fieldname",
"#": "&1.val",
"value": "&1.value"
}
}
},
{
"operation": "remove",
"spec": {
"*": {
"*": {
"value": ""
}
}
}
},
{
"operation": "default",
"spec": {
"*": {
"value": null
}
}
},
{
"operation": "modify-default-beta",
"spec": {
"*": {
"value?": "#(1,val)"
}
}
},
{
"operation": "remove",
"spec": {
"*": {
"val": ""
}
}
}
]

Moving one array into json objects defined in another array - JOLT Transformation

I have my input defined as
{
"lineId": "1",
"Collection": {
"services": [
{
"Code": "TB",
"Type": [
"Data"
]
},
{
"Code": "MAGTB",
"Type": [
"Data"
]
}
]
},
"promotions": [
{
"Id": "1"
},
{
"Id": "2"
}
]
}
I would like to get my output as
{
"lineId": "1",
"Collection": {
"services": [
{
"Code": "TB",
"Type": [
"Data"
],
"promotions": [
{
"Id": "1"
},
{
"Id": "2"
}
]
},
{
"Code": "TB2",
"Type": [
"Data"
],
"promotions": [
{
"Id": "1"
},
{
"Id": "2"
}
]
}
]
}
}
Any help would be appreciated.
I am new to JOLT. And I'm having trouble navigating to the second array from inside the first.
Incomplete transformation that I tried:
[
{
"operation": "shift",
"spec": {
"Collection": {
"services": {
"*": "Collection.services[].&",
"#(3,lineId)": "lineId",
"#(3,promotions)":{
"*":
}
}
}
}
}
]
edit: Tried this now
[
{
"operation": "shift",
"spec": {
"Collection": {
"services": {
"*": "Collection.services[]",
// "*": "&",
"#(3,lineId)": "lineId",
"#(3,promotions)": {
"*": {
"Id": "Id"
}
}
}
}
}
}
]
I just have to figure out a way to move the Id list inside the objects in services array.
edit2:
[
{
"operation": "shift",
"spec": {
"Collection": {
"services": {
"*": {
"*": "Collection.services[&1].&",
"#(3,lineId)": "Collection.services[&1].lineId",
"#(3,promotions)": "Collection.services[&1].promotions"
}
}
}
}
}
]
I think this is the spec you're looking for?
[
{
"operation": "shift",
"spec": {
"lineId": "lineId",
"Collection": {
"services": {
"*": {
"#(3,promotions)": "Collection.services[&1].promotions",
"*": "Collection.services[&1].&"
}
}
}
}
}
]

Jolt giving values as root

Here's my input data :
[
{
"TEST1": "abcd",
"TEST2": "xyz",
"TEST3": "08"
},
{
"TEST1": "abcd",
"TEST2": "xyz",
"TEST3": "20"
}
]
Output
[
{
"TEST1": "root",
"TEST2": "xyz",
"TEST3": ["08","20"]
}
]
Expected Output
[
{
"TEST1": "abcd",
"TEST2": "xyz",
"TEST3": ["08","20"]
}
]
Jolt specs
[
{
"operation": "shift",
"spec": {
"": {
"TEST3": "#(1,TEST2).TEST3"
}
}
},
{
"operation": "shift",
"spec": {
"": {
"$1": "[#2].TEST1",
"$": "[#2].TEST2",
"#.TEST3": "[#2].TEST3"
}
}
}
]
Can you please help me in achieving the excpected output
Check this spec
[
//Concat the TEST1 and TEST2 to temp
{
"operation": "modify-default-beta",
"spec": {
"*": {
"temp": "=concat(#(1,TEST1),':',#(1,TEST2))"
}
}
},
//Group the values by shifting TEST3
{
"operation": "shift",
"spec": {
"*": {
"TEST3": "#(1,temp)"
}
}
},
// Assign the keys to the node named key and value to the node nameed value
{
"operation": "shift",
"spec": {
"*": {
"#": "[#2].key",
"$": "[#2].value"
}
}
},
//Shift and assign the respective values to the TEST1, TEST2 and TEST3
{
"operation": "shift",
"spec": {
"*": {
"value": {
"*:*": {
"$(0,1)": "[&3].TEST1",
"$(0,2)": "[&3].TEST2"
}
},
"key": "[&1].TEST3"
}
}
}
]

Set null if value is not exists, otherwise return existing value if exists in jolt

As shown in sample input, I have array of extension in each object. When extension doesn't consists subscription-type then output should be null as shown in output or else should consist existing value. Same should be applicable for language-type. Order of subscription-type and language-type is random in nature.
I have tried with spec but its not working
{
"operation": "modify-define-beta",
"spec": {
"*": {
"subscriptionType": {
"url": {
"myCoercedValue": "subscription-type",
"myStringValue": "subscription-type"
},
"value": {
"myCoercedValue": null,
"myStringValue": null
}
},
"languageType": {
"url": {
"myCoercedValue": "language-type",
"myStringValue": "language-type"
},
"value": {
"myCoercedValue": null,
"myStringValue": null
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"extension": "[&1].extension",
"subscriptionType": "[&1].extension[]",
"languageType": "[&1].extension[]"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"extension": {
"*": {
"#url": {
"myStringValue": {
"subscription-type": {
"#(3,value.myStringValue)": "[&3].subscriptionType[]"
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"0": "[]"
}
}
]
Input:
[
{
"extension": []
},
{
"extension": [
{
"url": {
"myCoercedValue": "subscription-type",
"myStringValue": "subscription-type"
},
"value": {
"myCoercedValue": "free",
"myStringValue": "free"
},
"extension": []
}
]
},
{
"extension": [
{
"url": {
"myCoercedValue": "language-type",
"myStringValue": "language-type"
},
"value": {
"myCoercedValue": "en-us",
"myStringValue": "en-us"
}
},
{
"url": {
"myCoercedValue": "subscription-type",
"myStringValue": "subscription-type"
},
"value": {
"myCoercedValue": "free",
"myStringValue": "free"
}
}
]
}
]
Output:
[ {
"subscriptionType" : [ null, "free", "free" ],
"language":[null,null,"en-US"]
} ]
Perhaps you want to get such a result? Please, try those spec and say what you want to modify.
[
{
"operation": "shift",
"spec": {
"*": {
"extension": {
"*": {
"url": {
"myCoercedValue": {
"subscription-type": {
"#(3,value)": {
"myCoercedValue": "subscriptionType[#8]"
}
},
"language-type": {
"#(3,value)": {
"myCoercedValue": "language[#8]"
}
}
}
}
}
}
}
}
}
]