Home > Net >  Jolt Transformation: Make the nested object field to be part of main Json Object and also convert th
Jolt Transformation: Make the nested object field to be part of main Json Object and also convert th

Time:01-16

I am new to JOLT transformation, can someone help me in transforming the Input Json to the desired output?

Input JSON:

[ { "product_id": 1, "product_description": "Product 1 details", "billing_details": { "product_id": 1, "billing_description": "Product 1 billing Details" }, "product_part_desc": { "id": 1, "part_description": "product 1 part description" } }, { "product_id": 2, "product_description": "Product 2 details", "billing_details": { "product_id": 1, "billing_description": "Product 2 billing Details" }, "product_part_desc": { "id": 1, "part_description": "product 2 part description" } } ]

Tried Specification [ { "operation": "shift", "spec": { "": { "product_id": "[&1].id", "product_description": "[&1].description", "billing_details": { "billing_description": "[&2].bill_desc" }, "": "&" } } }, { "operation": "modify-overwrite-beta", "spec": { "*": { "product_part_desc": "=toString(@(1,product_part_desc))" } } } ]

Expected output

[ { "id": 1, "product_description": "Product 1 details", "billing_description": "Product 1 billing Details", "product_part_desc": "{id=1, part_description=product 1 part description}" }, { "product_id": 2, "product_description": "Product 2 details", "billing_description": "Product 2 billing Details", "product_part_desc": "{id=1, part_description=product 2 part description}" } ]

But the above spec is giving below output enter image description here

CodePudding user response:

You can use this spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "product_id": "[&1].id",
        "product_description": "[&1].description",
        "billing_details": {
          "billing_description": "[&2].bill_desc"
        },
        "product_part_desc": {
          "*": "[&2].product_part_desc"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "product_part_desc": "=join(', part_description=',@0)",
        "product_part_des*": "=concat('{id=',@0,'}')"
      }
    }
  }
]

You can change the keys as you want in the above code.

CodePudding user response:

You can use

  • a concat function within a modify spec in order to get product_part_desc attribute
  • a @(0,billing_details.billing_description) identifier along with a shift transformation spec to get billing_description attribute

as result :

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "product_part_desc": "=concat('',@(1,&))"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "product_id": "[&1].id",
        "product_*": "[&1].&",
        "@(0,billing_details.billing_description)": "[&1].billing_description"
      }
    }
  }
]
  • Related