Conditional shift within array with Jolt - json

Thanks in advance for reading this one!
I have the following JSON:
{
"transactions": [
{
"type": "wd",
"charged": "-1000.00",
"credited": "0"
},
{
"type": "dep",
"charged": "0",
"credited": "1000.00"
}
]
}
What I need is to keep either charged or credited, whichever one is not 0. My spec below almost has it working
[
{
"operation": "shift",
"spec": {
"transactions": {
"*": {
"type": "settled.[&1].transType",
"charged": {
"0": {
"#(2,credited)": "settled.[&1].cash_movement"
}
},
"credited": {
"0": {
"#(2,charged)": "settled.[&1].cash_movement"
}
}
}
}
}
}
]
What I want to end up with is:
{
"settled": [
{
"transType": "wd",
"cash_movement": "-1000.00"
},
{
"transType": "dep",
"cash_movement": "1000.00"
}
]
}
But what I end up with is this instead:
{
"settled": [
{
"transType": "wd",
"cash_movement": ["-1000.00","1000.00"]
},
{
"transType": "dep"
}
]
}
I know I'm close but for the life of me, I haven't been able to tweak it to get it just right. Can someone set me in the right direction please?

Just need to convert the identifiers [&1] to [&3] except for the first one such as
[
{
"operation": "shift",
"spec": {
"transactions": {
"*": {
"type": "settled[&1].transType",
"charged": {
"0": {
"#(2,credited)": "settled[&3].cash_movement"
}
},
"credited": {
"0": {
"#(2,charged)": "settled[&3].cash_movement"
}
}
}
}
}
}
]
since twice more going the tree up needed for those innermost pairs in order to reach the same level with the first [&1]. Btw, the dots between settled and [&.]s are redundant to be used.
the demo on the site http://jolt-demo.appspot.com/ is

Related

How does the jolt wildcards work for RHS?

I'm trying to understand the working of '#' wild card.
I have used the '#' in the spec while preparing the productList and it works and I got the output as per my expectation. But I'm not sure about the working of it.
Can anyone please help me to understand the working of it?
Here is the input JSON
{
"orders": [
{
"order_parts": [
{
"id": "0001",
"items": [
{
"id": "00101",
"goodIdentificationList": [
{
"goodIdentificationTypeId": "UPCA",
"idValue": "42684666380437"
},
{
"idValue": "V-ASHBY",
"goodIdentificationTypeId": "SHOPIFY_PROD_SKU"
}
],
"productName": "BLACK / 6 / 809"
},
{
"id": "00102",
"goodIdentificationList": [
{
"goodIdentificationTypeId": "SHOPIFY_PROD_ID",
"idValue": "42684666380437"
},
{
"idValue": "V-ASHBY",
"goodIdentificationTypeId": "UPCA"
}
],
"productName": "BLACK / 6 / 809"
}
]
},
{
"id": "0002",
"items": [
{
"id": "00103",
"goodIdentificationList": [
{
"goodIdentificationTypeId": "SHOPIFY_PROD_ID",
"idValue": "42684666380437"
},
{
"idValue": "V-ASHBY",
"goodIdentificationTypeId": "UPCA"
}
],
"productName": "BLACK / 6 / 809"
}
]
}
]
}
]
}
Expected by the below spec:-
Check every map of goodIdentificationList, get the idValue where goodIdentificationTypeId - UPCA, and put in the productList as gtin.
Get the id from the items list and put it in the productList as an itemId.
Get the productName from the items list and put it in the productList as a name.
Jolt Spec is like below
[
{
"operation": "shift",
"spec": {
"orders": {
"*": {
"order_parts": {
"*": {
"items": {
"*": {
"goodIdentificationList": {
"*": {
"goodIdentificationTypeId": {
"UPCA": {
"#(2,idValue)": "[&5].productList.[#8].gtin"
}
}
}
},
"id": "[&1].productList.[#4].itemId",
"productName": "[&1].productList.[#4].name"
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"productList": {
"*": "productList.[]"
}
}
}
}
]
By the above spec, I'm able to prepare the productList as I was expecting.
But want to understand the working of '#' here.
Output JSON
{
"productList" : [ {
"itemId" : "00101",
"name" : "BLACK / 6 / 809"
}, {
"itemId" : "00103",
"name" : "BLACK / 6 / 809"
}, {
"itemId" : "00102",
"name" : "BLACK / 6 / 809"
} ]
}
Any help will be appreciated.
Thanks!
You principally need this spec
[
{
"operation": "shift",
"spec": {
"order*": {
"*": {
"order*": {
"*": {
"items": {
"*": {
"id": "&3.&1.&",
"prod*": "&3.&1.&"
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "productList[]"
}
}
}
]
in which, the combination of the indexes of "items" array(&1) vs. the indexes of "order_parts" array(&3) handles separation of three individual objects, and then then tiding up innermost part while tagging the desired value(productList), and combining those newly generated objects will spontaneously form an array.
Replacement of
"id": "&3.&1.&",
"prod*": "&3.&1.&"
with
"id": "&3[&1].&",
"prod*": "&3[&1].&"
or
"id": "&3[#2].&",
"prod*": "&3[#2].&"
would handle exactly the same for this current case. But if there was no array the identifiers [&1] or [#2] would produce square brackets. The difference for right-hand-size usage of them is [&1] will traverse {, while [#2] will traverse both { and the current : characters in order to reach the target within the current tree, and [&1] will use indexes 0,1,2, which starts from zero, everytime and this will generate some null components for generated arrays for some cases, while [#2] won't. Eg. On the RHS of the spec, # is only valid in the the context of an array, like "[#2]".What "[#2]" means is, go up the three levels(including the colon at the current level) and ask that node how many matches it has had, and then use that as an index in the arrays.

Using variable from previous spec block in JOLT

I need to convert input attributes into SOLR request containing three sections, one of them is solrOptions.
in my request I have:
{
...
"firstTranDateRange": "IN LAST 365 DAYS",
"lastTranDateRange": "In last 60 days",
...
}
In the output I need to have following:
{
"searchAttributes": { ... }
"searchOptions": { ... }
"solrOptions: {
"anm_boost" : "1.0",
"pnm_boost" : "1.0",
"lastTranDays" : {
"min" : "00000",
"max": "00060"
},
"firstTranDays" : {
"min" : "00000",
"max" : "00365"
}
}
}
I wrote the following spec:
[
{
"spec": {
"searchAttrbutes": {
// filling up some other attributes not related to the question
"ltdr": "=substring(#(1,lastTranDateRange),8,3)",
"ltdr1": "=trim",
"ftdr": "=substring(#(1,firstTranDateRange),8,3)",
"ftdr1": "=trim"
}
},
"operation": "modify-default-beta"
},
{
"spec": {
"solrOptions": {
"anm_boost": "1.0",
"pnm_boost": "1.0"
},
"searchOptions": {
"maxRecords": 1,
...
}
},
"operation": "default"
},
{
"spec": {
"lastTranDateRange": {
"#00000": "solrOptions.lastTranDays.min",
"#(2,ltdr1)": "solrOptions.lastTranDays.max"
},
"firstTranDateRange": {
"#00000": "solrOptions.firstTranDays.min",
"#(2,ftdr1)": "solrOptions.firstTranDays.max"
},
...
},
"operation": "shift"
}
]
I haven't figured out how to prepend number with proper number of zeros. Help in this will be also appreciated. However, it is not working in the first place. The variables I defined do not appear in the output. In the output I am getting
{
"solrOptions": {
"anm_boost": "1.0",
"pnm_boost": "1.0",
"wcNameOn": "true",
"fuzzyNameOn": "true",
"lastTranDays": {
"min": "00000"
},
"firstTranDays": {
"min": "00000"
}
},
"searchOptions": {
"maxRecords": "10",
"matchIndicators": "TRUE",
...
}
}
Can someone please tell me what I am doing incorrectly.
Thanks.
You can start with getting rid of "searchAttributes" from modify transformation presuming the input is exactly
{
"firstTranDateRange": "IN LAST 365 DAYS",
"lastTranDateRange": "In last 60 days"
}
otherwise keep it, and converting that from default to overwrite in order to overwrite all existing attributes. Btw, substring in Jolt is interestingly different function unlike to their usage in some well-known DBMS products, here ending index should be provided as the last argument.
Moreover leftPad function will be needed for your case to prepend the values with zeroes.
* wildcard is used to represent all or rest of the expressions(key names), and &1 to climb one(1) level tree up and grab the related key name(solrOptions)
Thus, you can use the following combination of specs:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=substring(#(1,&),8,11)" // overwrite all attributes at once by using * and & wildcards
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"lastTranDateRange": "=trim", //individual manipulation needed as not having 3 digits
"*": "=leftPad(#(1,&),5,'0')"
}
},
{
"operation": "default",
"spec": {
"solrOptions": {
"anm_boost": "1.0",
"pnm_boost": "1.0"
},
"searchOptions": {
"maxRecords": 1
}
}
},
{
"operation": "shift",
"spec": {
//"searchAttributes": "&",
"search*": "&",
"solr*": {
"*": "&1.&",
"#(1,firstTranDateRange)": "&1.firstTranDays.max",
"#(1,lastTranDateRange)": "&1.lastTranDays.max"
}
}
},
{
"operation": "default",
"spec": {
"solrOptions": {
"firstTranDays": {
"min": "00000"
},
"lastTranDays": {
"min": "00000"
}
}
}
}
]
to yield the output :
{
//"searchAttributes": "{....}",
"solrOptions" : {
"firstTranDays" : {
"max" : "00365",
"min" : "00000"
},
"lastTranDays" : {
"max" : "00060",
"min" : "00000"
},
"pnm_boost" : "1.0",
"anm_boost" : "1.0"
},
"searchOptions" : {
"maxRecords" : 1
}
}

JOLT shift transformation to filter values in array

I want to use a JOLT transformation to do two things:
filter the elements in the array called myarray so that only elements remain which have a "v_518" attribute
filter out all attributes of the remaining elements except for "v_518" and "lfdn"
Input:
{
"isError": false,
"isValid": true,
"myarray": [
{
"p_0001": "1",
"p_0002": "1",
"p_0003": "1",
"p_0004": "1",
"v_518": "0,214506186",
"lfdn": 89709
},
{
"p_0001": "2",
"p_0002": "1",
"p_0003": "1",
"v_518": "0,3823236",
"lfdn": 89710
},
{
"p_0001": "3",
"p_0002": "1",
"p_0003": "1",
"lfdn": 89711
}
],
"errorMessage": null,
"exceptionMessage": null,
"innerExceptionMessage": null
}
Desired output:
{
"isError": false,
"isValid": true,
"myarray": [
{
"v_518": "0,214506186",
"lfdn": 89709
},
{
"v_518": "0,3823236",
"lfdn": 89710
}
],
"errorMessage": null,
"exceptionMessage": null,
"innerExceptionMessage": null
}
What I tried so far, but doesn't work as intended:
[
{
"operation": "shift",
"spec": {
"isError": "isError",
"isValid": "isValid",
"myarray": {
// loop thru all the elements in value array
"*": {
"v_518": {
// if the value "v_518" exists
// grab the whole object and write it out to
// a v_518_array array.
"#(1,v_518)": "v_518_array",
"#(1,lfdn)": "v_518_array"
}
}
},
"errorMessage": "errorMessage",
"exceptionMessage": "exceptionMessage",
"innerExceptionMessage": "innerExceptionMessage"
}
}
]
I tried working with the examples in http://jolt-demo.appspot.com/#andrewkcarter2 but I couldn't figure out how to do it.
I was able to solve my issue. This answer was the hint I needed to get the ball rolling: https://stackoverflow.com/a/38154541/1561441
The key is referencing the array you are currently transforming via "value" = "array[&1].value".
In my mind I spent way too much time on this issue. Does anyone know of a good documentation for the Jolt syntax? I couldn't find a satisfactory one by googling myself.
[
{
"operation": "shift",
"spec": {
"isError": "isError",
"isValid": "isValid",
"myarray": {
// loop thru all the elements in value array
"*": {
"v_518": {
// if the value "v_518" exists
// grab the whole object and write it out to
// a v_518_array array.
"#1": "v_518_array"
}
}
},
"errorMessage": "errorMessage",
"exceptionMessage": "exceptionMessage",
"innerExceptionMessage": "innerExceptionMessage"
}
},
{
"operation": "shift",
//Transform array: https://stackoverflow.com/questions/37865871/how-do-i-transform-an-array-using-jolt
"spec": {
"v_518_array": {
// loop thru all the elements in value array
"*": {
"v_518": "v_518_array[&1].v_518",
"lfdn": "v_518_array[&1].lfdn"
}
}
}
}
]
Here's a slightly better solution:
[
{
"operation": "shift",
"spec": {
"isError": "isError",
"isValid": "isValid",
"myarray": {
// loop thru all the elements in value array
"*": {
"v_518": {
// if the value "v_518" exists
// grab the whole object and write it out to
// a v_518_array array.
"#1": "v_518_array"
}
}
},
"errorMessage": "errorMessage",
"exceptionMessage": "exceptionMessage",
"innerExceptionMessage": "innerExceptionMessage"
}
},
{
"operation": "shift",
//Transform array: https://stackoverflow.com/questions/37865871/how-do-i-transform-an-array-using-jolt
"spec": {
"v_518_array": {
// loop thru all the elements in value array
"*": {
"v_518": "&2[&1].v_518", //notice the generic shorthand here
"lfdn": "&2[&1].lfdn"
}
}
}
}
]
Still there's a briefer, and more dynamic method to figure out your issue such as
[
{
"operation": "shift",
"spec": {
"myarray": {
"*": {
"v_518": {
"#(1,&)": "&3.[&2].&",
"#(1,lfdn)": "&3.[&2].lfdn"
}
}
},
"*": "&"
}
}
]

Jolt Transformation - json contains resut format

The body of the request consists of 2 objects, one of which is the payload, and the other is a description, since the payload must be transformed for transmission to the next system.
Please, help write a specification for such a format:
body:
{
"input":{
"items":[
{
"id":1,
"name":"items1",
"statusId":5
},
{
"id":7,
"name":"items7",
"statusId":2
},
...
{
"id":N, // any number
"name":"itemsN",
"statusId":1
}
]
},
"output":[
{
"target":"sg_id",
"source":"id",
"type":"list"
},
{
"target":"sg_name",
"source":"name",
"type":"list"
}
]}
result:
{
"items":{
"sg_id":[
1,
7,
...
N
],
"sg_name":[
"items1",
"items7",
...
"itemsN"
]
}}
You can use a shift operation through prepending desired aliases with the title(item.) of the array such as
[{
"operation": "shift",
"spec": {
"input": {
"items": {
"*": {
"id": "items.sg_id",
"name": "items.sg_name"
}
}
}
}
}]

How tracnform rest of json into one field value using jolt?

Here is the JSON input:
{
"myRootKey": {
"directMove": "directValue",
"marker": "THE_MARKER",
"someTextField": "someString",
"someObject": {
"someKey": "value"
}
}
}
the output should be:
{
"myRootKey": {
"subKey": {
"directMove": "directValue"
},
"THE_MARKER": {
"someTextField": "someString",
"someObject": {
"someKey": "value"
}
}
}
}
With direct moving it is clear, but how rest of the input to the marker object value?
You match down to "someTextField" and "someObject", but use the new "#" / look up the tree logic to find the "marker" to use as an ouput path.
Spec
[
{
"operation": "shift",
"spec": {
"myRootKey": {
"directMove": "myRootKey.subKey.directValue",
"someTextField": "#(1,marker).someTextField",
"someObject": "#(1,marker).someObject"
}
}
}
]
#(1,marker) allows you to retrieve the value of the marker field
&1 retrieves the value of the matching node
So the spec you are looking for looks like :
[
{
"operation": "shift",
"spec": {
"myRootKey": {
"directMove": "myRootKey.subKey.directValue",
"someTextField": "&1.#(1,marker).someTextField",
"someObject": "&1.#(1,marker).someObject"
}
}
}
]
You can use this spec fully dynamically:
[
{
"operation": "shift",
"spec": {
"*": {
"directMove": "&1.subKey.&",
"*TextField": "&1.#(1,marker).&",
"*Object": "&1.#(1,marker).&"
}
}
}
]