I'm trying to do if condition in array but not able to get the expected output.
If "serviceType"
= "Remote Collect"
,
then shipments.details.container.containerItems.weight
=
waypoint.inventory.inventory.handling_units.remote_weight
else shipments.details.container.containerItems.weight
= waypoint.inventory.inventory.weight
Input JSON:
{
"serviceType": "Remote Collect",
"serviceLevel": "STANDARD_SERVICE",
"shipments": [
{
"details": [
{
"containers": [
{
"containerID": "container1",
"containerType": "Box1",
"contentDescription": "Fruits",
"quantity": 7,
"containerItems": [
{
"itemID": "strawberries",
"quantity": "1",
"itemDescription": "strawberries bag",
"weight": 23
}
]
}
]
}
]
}
]
}
Jolt spec:
[
{
"spec": {
"serviceType": {
"Remote Collect": {
"@(2,shipments[0].freightTotals.weight)": "total_handling_units.remote_weight",
"@(2,serviceType)": "service_type_tmp",
"#4": "task_type_id",
"#true": "remoteWeight_tmp"
},
"*": {
"@(2,shipments[0].freightTotals.weight)": "total_weight",
"@(2,serviceType)": "service_type_tmp",
"#4": "task_type_id",
"#false": "remoteWeight_tmp"
}
},
"shipments": {
"0": {
"details": {
"*": {
"containers": {
"*": {
"containerItems": {
"*": {
"itemDescription": [
"way_points[0].inventory[&3].inventory[&1].description",
"way_points[0].inventory[&3].inventory[&1].name"
],
"remoteWeight_tmp": {
"#true": {
"weight": [
"way_points[0].inventory[&3].inventory[&1].remote_weight"
]
},
"#false": {
"weight": [
"way_points[0].inventory[&3].inventory[&1].weight"
]
}
},
"itemID": [
"way_points[0].inventory[&3].inventory[&1].external_id"
],
"quantity": [
"way_points[0].inventory[&3].inventory[&1].original_quantity"
],
"#true": [
"way_points[0].inventory[&3].inventory[&1].pending"
]
}
}
}
}
}
}
}
}
},
"operation": "shift"
}
]
Actual Output:
{
"remoteWeight_tmp": "true",
"service_type_tmp": "Remote Collect",
"task_type_id": "4",
"way_points": [
{
"inventory": [
{
"inventory": [
{
"description": "strawberries bag",
"external_id": "strawberries",
"name": "strawberries bag",
"original_quantity": "1",
"pending": "true"
}
]
}
]
}
]
}
Expected Output:
{
"remoteWeight_tmp": "true",
"service_type_tmp": "Remote Collect",
"task_type_id": "4",
"way_points": [
{
"inventory": [
{
"inventory": [
{
"description": "strawberries bag",
"external_id": "strawberries",
"name": "strawberries bag",
"original_quantity": "1",
"pending": "true",
"handling_units": {
"remote_weight": 23
}
}
]
}
]
}
}
You can dive deep upto the innermost array(containerItems
) and tile the attributes such as
[
{
"operation": "shift",
"spec": {
"*T*": "&(0,1)_t&(0,2)_tmp",
"#true": "remoteWeight_tmp",
"#4": "task_type_id",
"shipments": {
"*": {
"details": {
"*": {
"containers": {
"*": {
"containerItems": {
"*": {
"itemD*": ["way_points[0].inventory[0].inventory[&1].d&(0,1)", "way_points[0].inventory[0].inventory[&1].name"], // to name double attribute by the same value
"itemID": "way_points[0].inventory[0].inventory[&1].external_id",
"q*": "way_points[0].inventory[0].inventory[&1].original_q&(0,1)", // replacement for the 1st asterisk from the 0th(current) level
"#true": "way_points[0].inventory[0].inventory[&1].pending",
"@8,serviceType": { // conditional follows
"Remote Collect": {
"@2,weight": "way_points[0].inventory[0].inventory[&2].handling_units.remote_weight" // grab the value after going 2 levels up
},
"*": { // else case
"@2,weight": "way_points[0].inventory[0].inventory[&2].weight"
}
}
}
}
}
}
}
}
}
}
}
},
{ // sort the results in alphabetical order based on the elements' keys
"operation": "sort"
}
]
the demo on the site http://jolt-demo.appspot.com/ is