bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.54k stars 329 forks source link

How to add nested values using JOLT #1071

Open tailangankur opened 3 years ago

tailangankur commented 3 years ago

Input JSON: [ { "itemId": "727", "transactionDate": "2021-03-15T16:28:02", "description": "727", "typeofreturn": "none", "prices_include_tax": true, "shipToAddress": { "line1": "969 Market", "line2": "", "city": "San Francisco", "state": "CA", "zip": "94103", "country": "US" }, "line_items": [ { "id": 315, "name": "Woo Single #1", "product_id": 93, "variation_id": 0, "quantity": 1, "tax_class": "", "subtotal": "6.00", "subtotal_tax": "0.45", "total": "7.00", "total_tax": "0.45", "taxes": [], "meta_data": [], "sku": "", "price": 3 } ], "tax_lines": [ { "id": 318, "rate_code": "US-CA-STATE TAX", "rate_id": 75, "label": "State Tax", "compound": false, "tax_total": "1.35", "shipping_tax_total": "0.00", "meta_data": [] } ], "shipping_lines": [ { "id": 317, "method_title": "Flat Rate", "method_id": "flat_rate", "total": "0.00", "total_tax": "0.00", "taxes": [], "meta_data": [] } ], "fee_lines": [], "coupon_lines": [], "refunds": [ { "id": 727, "refund": "", "total": "7.40", "refund_tax": "0.00" } ], "quantity": 1, "refund": false }, { "itemId": "728", "transactionDate": "2021-03-16T16:28:02", "description": "728", "typeofreturn": "none", "prices_include_tax": false, "shipToAddress": { "line1": "970 Market", "line2": "", "city": "Mountain View", "state": "CA", "zip": "94104", "country": "US" }, "line_items": [ { "id": 316, "name": "Woo Single #1", "product_id": 93, "variation_id": 0, "quantity": 1, "tax_class": "", "subtotal": "10.00", "subtotal_tax": "0.40", "total": "7.00", "total_tax": "0.40", "taxes": [], "meta_data": [], "sku": "", "price": 3 } ], "tax_lines": [ { "id": 318, "rate_code": "US-CA-STATE TAX", "rate_id": 75, "label": "State Tax", "compound": false, "tax_total": "1.35", "shipping_tax_total": "0.00", "meta_data": [] } ], "shipping_lines": [ { "id": 317, "method_title": "Flat Rate", "method_id": "flat_rate", "total": "0.00", "total_tax": "0.00", "taxes": [], "meta_data": [] } ], "fee_lines": [], "coupon_lines": [], "refunds": [ { "id": 727, "refund": "", "total": "7.40", "refund_tax": "0.00" } ], "quantity": 3, "refund": true }, { "itemId": "729", "transactionDate": "2021-03-17T16:28:02", "description": "729", "typeofreturn": "refunded", "prices_include_tax": true, "shipToAddress": { "line1": "971 Market", "line2": "", "city": "Mountain View", "state": "CA", "zip": "94104", "country": "US" }, "line_items": [ { "id": 316, "name": "Woo Single #1", "product_id": 93, "variation_id": 0, "quantity": 1, "tax_class": "", "subtotal": "10.00", "subtotal_tax": "0.40", "total": "7.00", "total_tax": "0.40", "taxes": [], "meta_data": [], "sku": "", "price": 3 } ], "shipping_lines": [ { "id": 317, "method_title": "Flat Rate", "method_id": "flat_rate", "total": "0.00", "total_tax": "0.00", "taxes": [], "meta_data": [] } ], "tax_lines": [ { "id": 318, "rate_code": "US-CA-STATE TAX", "rate_id": 75, "label": "State Tax", "compound": false, "tax_total": "1.35", "shipping_tax_total": "0.00", "meta_data": [] } ], "fee_lines": [], "coupon_lines": [], "refunds": [ { "id": 727, "refund": "", "total": "7.40", "refund_tax": "0.00" } ], "quantity": 1, "refund": false }, { "itemId": "730", "transactionDate": "2021-03-18T16:28:02", "description": "730", "typeofreturn": "none", "prices_include_tax": false, "shipToAddress": { "line1": "972 Market", "line2": "", "city": "Mountain View", "state": "CA", "zip": "94104", "country": "US" }, "line_items": [ { "id": 316, "name": "Woo Single #1", "product_id": 93, "variation_id": 0, "quantity": 1, "tax_class": "", "subtotal": "10.00", "subtotal_tax": "0.40", "total": "7.00", "total_tax": "0.40", "taxes": [], "meta_data": [], "sku": "", "price": 3 } ], "shipping_lines": [ { "id": 317, "method_title": "Flat Rate", "method_id": "flat_rate", "total": "0.00", "total_tax": "0.00", "taxes": [], "meta_data": [] } ], "tax_lines": [ { "id": 318, "rate_code": "US-CA-STATE TAX", "rate_id": 75, "label": "State Tax", "compound": false, "tax_total": "1.35", "shipping_tax_total": "0.00", "meta_data": [] } ], "fee_lines": [], "coupon_lines": [], "refunds": [ { "id": 727, "refund": "", "total": "7.40", "refund_tax": "0.00" } ], "quantity": 1, "refund": true } ]

Spec.json: [ { "operation": "shift", "spec": { "*": { "shipToAddress": { "@(1,typeofreturn)": { "none": { "@(2,line1)": "[&4].deliveryDetails.address.streetAddress[]", "@(2,line2)": "[&4].deliveryDetails.address.streetAddress[]", "@(2,city)": "[&4].deliveryDetails.address.locality", "@(2,state)": "[&4].deliveryDetails.address.region", "@(2,country)": "[&4].deliveryDetails.address.country", "@(2,zip)": "[&4].deliveryDetails.address.postalCode" } } }, "line_items": { "*": { "@(2,typeofreturn)": { "none": { "@(2,quantity)": "[&5].lineItems[&3].quantityOrdered", "@(2,total)": ["[&5].lineItems.[&3].price.value", "[&5].netPriceAmount.value"], "@(2,total_tax)": ["[&5].lineItems.[&3].tax.value", "[&5].netTaxAmount.value"] } } } }, "shipping_lines": { "*": { "@(2,typeofreturn)": { "none": { "@(2,total)": "[&5].shippingCost.value", "@(2,total_tax)": "[&5].shippingCostTax.value" } } } }, "refunds": { "*": { "@(2,refund)": { "true": { "@(2,total)": "[&5].refunds[&5].amount.value" } }, "@(2,typeofreturn)": { "refunded": { "@(4,itemId)": "[&5].orderId", "@(4,transactionDate)": "[&5].creationDate", "@(2,total)": "[&5].returnPricingInfo.totalRefundedAmount.priceAmount.value", "@(2,refund_tax)": "[&5].returnPricingInfo.totalRefundedAmount.taxAmount.value", "#12345": "[&5].orderReturnId" }, "*": { "@(4,itemId)": "[&5].id", "@(4,transactionDate)": "[&5].placedDate" } } } }, "description": "[&1].&" } } }, { "operation": "modify-overwrite-beta", "spec": { "*": "=recursivelySquashNulls" } } ]

Transformmed JSON: [ { "deliveryDetails" : { "address" : { "streetAddress" : [ "969 Market", "" ], "locality" : "San Francisco", "region" : "CA", "country" : "US", "postalCode" : "94103" } }, "lineItems" : [ { "quantityOrdered" : 1, "price" : { "value" : "7.00" }, "tax" : { "value" : "0.45" } } ], "netPriceAmount" : { "value" : "7.00" }, "netTaxAmount" : { "value" : "0.45" }, "shippingCost" : { "value" : "0.00" }, "shippingCostTax" : { "value" : "0.00" }, "id" : "727", "placedDate" : "2021-03-15T16:28:02", "description" : "727" }, { "deliveryDetails" : { "address" : { "streetAddress" : [ "970 Market", "" ], "locality" : "Mountain View", "region" : "CA", "country" : "US", "postalCode" : "94104" } }, "lineItems" : [ { "quantityOrdered" : 1, "price" : { "value" : "7.00" }, "tax" : { "value" : "0.40" } } ], "netPriceAmount" : { "value" : "7.00" }, "netTaxAmount" : { "value" : "0.40" }, "shippingCost" : { "value" : "0.00" }, "shippingCostTax" : { "value" : "0.00" }, "refunds" : [ { "amount" : { "value" : "7.40" } } ], "id" : "728", "placedDate" : "2021-03-16T16:28:02", "description" : "728" }, { "orderId" : "729", "creationDate" : "2021-03-17T16:28:02", "returnPricingInfo" : { "totalRefundedAmount" : { "priceAmount" : { "value" : "7.40" }, "taxAmount" : { "value" : "0.00" } } }, "orderReturnId" : "12345", "description" : "729" }, { "deliveryDetails" : { "address" : { "streetAddress" : [ "972 Market", "" ], "locality" : "Mountain View", "region" : "CA", "country" : "US", "postalCode" : "94104" } }, "lineItems" : [ { "quantityOrdered" : 1, "price" : { "value" : "7.00" }, "tax" : { "value" : "0.40" } } ], "netPriceAmount" : { "value" : "7.00" }, "netTaxAmount" : { "value" : "0.40" }, "shippingCost" : { "value" : "0.00" }, "shippingCostTax" : { "value" : "0.00" }, "refunds" : [ { "amount" : { "value" : "7.40" } } ], "id" : "730", "placedDate" : "2021-03-18T16:28:02", "description" : "730" } ]

now after transforming the json i want to perform overwrite-beta operation to "add" the lineItems->price->value with shippingCost->value to get the netPriceAmount->value update.

lucioalmeida commented 3 years ago

Spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "shipToAddress": {
          "@(1,typeofreturn)": {
            "none": {
              "@(2,line1)": "[&4].deliveryDetails.address.streetAddress[]",
              "@(2,line2)": "[&4].deliveryDetails.address.streetAddress[]",
              "@(2,city)": "[&4].deliveryDetails.address.locality",
              "@(2,state)": "[&4].deliveryDetails.address.region",
              "@(2,country)": "[&4].deliveryDetails.address.country",
              "@(2,zip)": "[&4].deliveryDetails.address.postalCode"
            }
          }
        },
        "line_items": {
          "*": {
            "@(2,typeofreturn)": {
              "none": {
                "@(2,quantity)": "[&5].lineItems[&3].quantityOrdered",
                "@(2,total)": [
                                    "[&5].lineItems.[&3].price.value",
                                    "[&5].netPriceAmount.value"
                                ],
                "@(2,total_tax)": [
                                    "[&5].lineItems.[&3].tax.value",
                                    "[&5].netTaxAmount.value"
                                ]
              }
            }
          }
        },
        "shipping_lines": {
          "*": {
            "@(2,typeofreturn)": {
              "none": {
                "@(2,total)": "[&5].shippingCost.value",
                "@(2,total_tax)": "[&5].shippingCostTax.value"
              }
            }
          }
        },
        "refunds": {
          "*": {
            "@(2,refund)": {
              "true": {
                "@(2,total)": "[&5].refunds[&5].amount.value"
              }
            },
            "@(2,typeofreturn)": {
              "refunded": {
                "@(4,itemId)": "[&5].orderId",
                "@(4,transactionDate)": "[&5].creationDate",
                "@(2,total)": "[&5].returnPricingInfo.totalRefundedAmount.priceAmount.value",
                "@(2,refund_tax)": "[&5].returnPricingInfo.totalRefundedAmount.taxAmount.value",
                "#12345": "[&5].orderReturnId"
              },
              "*": {
                "@(4,itemId)": "[&5].id",
                "@(4,transactionDate)": "[&5].placedDate"
              }
            }
          }
        },
        "description": "[&1].&"
      }
    }
    },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
    },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "lineItems": {
          "*": {
            "price": {
              "value": "=doubleSum(@(1,value), @(4,shippingCost.value))"
            }
          }
        }
      }
    }
    }
]