skip to Main Content

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 the below output
enter image description here

2

Answers


  1. 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.

    Login or Signup to reply.
  2. You can use:

    • a concat function within a modify spec in order to get
      product_part_desc attribute
    • a @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('',@0)"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*id": "[&1].id",
            "p*": "[&1].&",
            "@billing_details.billing_description": "[&1].billing_description"
          }
        }
      },
      {// this spec is added only to sort the attributes as desired
        "operation": "shift",
        "spec": {
          "*": {
            "id": "[&1].&",
            "product_description": "[&1].&",
            "billing_description": "[&1].&",
            "product_part_desc": "[&1].&"
          }
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search