Jolt update all 0 value to 1 - json

I would like to know in Jolt, how do I count the number of levels I need to moved up to get the required data.
I played around the Jolt spec to convert value in "quantity" from 0 to 1
Input
{
"items": [
{
"product": {
"name": "product1",
"id": "001"
},
"quantity": 1
},
{
"product": {
"name": "product2",
"id": "002"
},
"quantity": 0
},
{
"product": {
"name": "product3",
"id": "003"
},
"quantity": 0
}
]
}
The expected output
{
"items": [
{
"product": {
"name": "product1",
"id": "001"
},
"quantity": 1
},
{
"product": {
"name": "product2",
"id": "002"
},
"quantity": 1
},
{
"product": {
"name": "product3",
"id": "003"
},
"quantity": 1
}
]
}
Jolt spec with some notes for what I understand.
[
{
"operation": "shift",
"spec": {
"items": {
"*": {
"quantity": {
"0": {
"#1": "items.[&3].quantity" //[&3] => I move 3 levels up to items.* to get index?
},
"*": {
"#(2,quantity)": "items.[&3].quantity" //#(2,quantity) => I move 2 levels up to get items.*.quantity value?
}
},
"*": "items.[&1].&" //[&1] => I move 1 level up to items.* to get index?
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"items": {
"*": {
"quantity": "=toInteger(#(1,quantity))"
}
}
}
}
]
Do I understand it correctly? Please advice.
Thanks you

As much as I understand, you want to reflect the first quantity value within the items array to all other quantity values of the other objects. Then you can use these shift transformations :
[
{
"operation": "shift",
"spec": {
"items": {
"0": {
"#": "&"
},
"*": {
"#(0,product)": "&.product",
"#(2,&1[0].quantity)": "&.quantity"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "items"
}
}
]

Related

Create array of simpler JSON objects from a nested JSON using jolt

My input JSON is like
{
"common": {
"name": "abc"
},
"details": {
"id": 4,
"node": [
{
"name": "node1",
"array2": []
},
{
"name": "node2",
"array2": [
{
"name": "node2_a2_1"
}
]
},
{
"name": "node3",
"array2": [
{
"name": "node3_a2_1"
},
{
"name": "node3_a2_2"
},
{
"name": "node3_a2_3"
}
]
}
]
}
}
What I want is for each leaf node in array2 (e.g. {"name": "node3_a2_1"}) I will traverse towards the root and add all common items and create an array of JSON objects without any nested object. So, the output I want is like
[
{
"common_name": "abc",
"id": 4,
"node_name": "node2",
"name": "node2_a2_1"
},
{
"common_name": "abc",
"id": 4,
"node_name": "node3",
"name": "node3_a2_1"
},
{
"common_name": "abc",
"id": 4,
"node_name": "node3",
"name": "node3_a2_2"
},
{
"common_name": "abc",
"id": 4,
"node_name": "node3",
"name": "node3_a2_3"
}
]
Could you please suggest how can I do that?
You can walk through the array2 while picking values of each element from their original location in the JSON value.
For example; traverse } four times to grab the value of id by using #(4,id), and use #(3,name)[&1] as the common distinguishing identifier for each attribute such as
[
{
"operation": "shift",
"spec": {
"details": {
"node": {
"*": {
"array2": {
"*": {
"#(5,common.name)": "#(3,name)[&1].common_name",
"#(4,id)": "#(3,name)[&1].id",
"#(2,name)": "#(3,name)[&1].node_name",
"name": "#(3,name)[&1].&"
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": ""
}
}
}
]

Jolt unnest list to top level

I have a JSON:
[
{
"id": 1015380,
"type": "campaign",
"stats": [
{
"message_sends_by_any_user": 13,
"ctr": "0.094",
}
]
},
{
"id": 1015695,
"type": "campaign",
"stats": [
{
"message_sends_by_any_user": 7,
"ctr": "0.091",
}
]
}
]
I want to "unnested" stats list to top level. JOLT config:
[
{
"operation": "shift",
"spec": {
"*": {
"id": "[&1].id",
"type": "[&1].type",
"stats": {
"*": "[&2].&"
}
}
}
}
]
Expected:
[
{
"id": 1015380,
"type": "campaign",
"message_sends_by_any_user": 13,
"ctr": "0.094",
},
{
"id": 1015695,
"type": "campaign",
"message_sends_by_any_user": 7,
"ctr": "0.091"
}
]
But actual output contains array indexes like this:
[
{
"id" : 1015380,
"type" : "campaign",
"0" : {
"message_sends_by_any_user" : 13,
"ctr" : "0.094"
}, ...
How to avoid these indexes?
You can prefer walking through the indexes of the stats list by taking id and type attributes inside such as
[
{
"operation": "shift",
"spec": {
"*": {
"stats": {
"*": {
"#(2,id)": "[&3].id",
"#(2,type)": "[&3].type",
"*": "[&3].&"
}
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com is

Applying cardinality for multiple columns in Jolt

I am trying to apply Jolt for below data
input:
[
{
"id": "500",
"code": "abc",
"date": "2020-10-10",
"category": 1,
"amount": 100,
"result": 0
},
{
"id": "500",
"code": "abc",
"date": "2020-10-10",
"category": 2,
"amount": 200,
"result": 1
}
]
jolt used:
[
{
"operation": "shift",
"spec": {
"*": {
"id": "#(1,id).id",
"code": "#(1,id).code",
"date": "#(1,id).group1.date",
"category": "#(1,id).group1.group2[&1].category"
}
}
},
{
"operation": "cardinality",
"spec": {
"*": {
"id": "ONE"
}
}
},
{
"operation": "shift",
"spec": {
"*": ""
}
}
]
current output:
{
"id": "500",
"code": [
"abc",
"abc"
],
"group1": {
"date": [
"2020-10-10",
"2020-10-10"
],
"group2": [
{
"category": 1
},
{
"category": 2
}
]
}
}
expected:
{
"id": "500",
"code": "abc",
"group1": {
"date": "2020-10-10",
"group2": [
{
"category": 1
},
{
"category": 2
}
]
}
}
If i keep column of code & date in cardinality, it's fine. But in my use case, there are multiple such columns to be added. Are there any better ways to handle this scenario?
You should add each added node and use "*" wildcard to represent the rest of the attributes within the cardinality transformation such as
{
"operation": "cardinality",
"spec": {
"*": {
"*": "ONE",
"group1": {
"*": "ONE",
"group2": "MANY"
}
}
}
}
where "group2": "MANY" will make group2 to be excepted for extracting only the first element of the respective list.
the demo on the site http://jolt-demo.appspot.com/ :

JOLT transform object into an array

I am trying to transform the following object into an array, the object is formatted already as required just needs to be output as an array containing that object.
[
{
"PLCTime": 1643804542000,
"LevelID": "53.99.2",
"Data1Type": "Axis1 Dist",
"Data1": 1,
"Data2Type": "Axis2 Dist",
"Data2": 2,
"Data3Type": "Axis3 Dist",
"Data3": 3,
"Data4Type": "Axis4 Dist",
"Data4": 4,
"Data5Type": "Axis5 Dist",
"Data5": 5.5,
"Data6Type": "Axis6 Dist",
"Data6": 6
}
]
I would like the output to be an array containing the one object. Currently my spec is:
[
{
"operation": "shift",
"spec": {
"*": {
"Data*Type": {
"#(0)": "name"
},
"Data*": {
"#(0)": "value"
},
"*": "&"
}
}
},
{
"operation": "shift",
"spec": {
"LevelID": "assetId",
"PLCTime": "dataPoints[].timestamp",
"name": {
"*": {
"#": "dataPoints[0].measures[&].&2",
"#(3,value[&])": "dataPoints[0].measures[&].value"
}
}
}
}
]
Which gets me the following, but you can see the result is not an array.
{
"assetId": "53.99.2",
"dataPoints": [
{
"timestamp": 1643804542000,
"measures": [
{
"name": "Axis1 Dist",
"value": 1
},
{
"name": "Axis2 Dist",
"value": 2
},
{
"name": "Axis3 Dist",
"value": 3
},
{
"name": "Axis4 Dist",
"value": 4
},
{
"name": "Axis5 Dist",
"value": 5.5
},
{
"name": "Axis6 Dist",
"value": 6
}
]
}
]
}
The output I am trying for is :
[
{
"assetId": "53.99.2",
"dataPoints": [
{
"timestamp": 1643804542000,
"measures": [
{
"name": "Axis1 Dist",
"value": 1
},
{
"name": "Axis2 Dist",
"value": 2
},
{
"name": "Axis3 Dist",
"value": 3
},
{
"name": "Axis4 Dist",
"value": 4
},
{
"name": "Axis5 Dist",
"value": 5.5
},
{
"name": "Axis6 Dist",
"value": 6
}
]
}
]
}
]
e.g which needs to be enclosed in a set of []
You can
prefix the identifiers on the right-hand side with [0]. for the
second tranformation spec in order to nest the whole content within
the square brackets such as
{
"operation": "shift",
"spec": {
"LevelID": "[0].assetId",
"PLCTime": "[0].dataPoints[].timestamp",
"name": {
"*": {
"#": "[0].dataPoints[0].measures[&].&2",
"#(3,value[&])": "[0].dataPoints[0].measures[&].value"
}
}
}
}
or
add the following spec to the current ones, alternatively
{
"operation": "shift",
"spec": {
"#": "[]"
}
}

Jolt Conversion - iterate list within a list and form single list

I am trying to iterate lists inside a list and form a single list with multiple objects. Iterating lists, I am able to achieve. But applying tags before the list iterating to each object is not happening if there are more objects in single list.
My input request is like below:
[
{
"success": [
{
"id": "4",
"Offers": [
{
"name": "Optional",
"type": {
"id": "1",
"name": "Optional"
},
"productOfferings": [
{
"id": "3",
"name": "Test1"
}
]
},
{
"name": "Default",
"type": {
"id": "2",
"name": "Default"
},
"productOfferings": [
{
"id": "1",
"name": "Test2"
},
{
"id": "2",
"name": "Test3"
}
]
}
]
}
]
}
]
My spec is like below:
[
{
"operation": "shift",
"spec": {
"*": {
"success": {
"*": {
"Offers": {
"*": {
"name": "[&1].[&3].typeName",
"type": {
"id": "[&2].[&4].typeNameId",
"name": "[&2].[&4].typeNameValue"
},
"productOfferings": {
"*": {
"id": "[&3].[&1].id",
"name": "[&3].[&1].name"
}
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "[]"
}
}
}
]
Output Received from spec:
[
{
"typeName": "Optional",
"typeNameId": "1",
"typeNameValue": "Optional",
"id": "3",
"name": "Test1"
},
{
"typeName": "Default",
"typeNameId": "2",
"typeNameValue": "Default",
"id": "1",
"name": "Test2"
},
{
"id": "2",
"name": "Test3"
}
]
But Expected output is like below:
[
{
"typeName": "Optional",
"typeNameId": "1",
"typeNameValue": "Optional",
"id": "3",
"name": "Test1"
},
{
"typeName": "Default",
"typeNameId": "2",
"typeNameValue": "Default",
"id": "1",
"name": "Test2"
},
{
"typeName": "Default",
"typeNameId": "2",
"typeNameValue": "Default",
"id": "2",
"name": "Test3"
}
]
If there are more objects inside productOfferings object, I am not able to add typeName,typeNameId, typeNameValue to the actual object. Please help to fix this issue.
You seem just needing to collect all into the productOfferings array while prepending each key by the common identifier [&3].[&1] such as
[
{
"operation": "shift",
"spec": {
"*": {
"success": {
"*": {
"Offers": {
"*": {
"productOfferings": {
"*": {
"#(2,name)": "[&3].[&1].typeName",
"#(2,type.id)": "[&3].[&1].typeNameId",
"#(2,type.name)": "[&3].[&1].typeNameValue",
"*": "[&3].[&1].&"
}
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "[]"
}
}
}
]