Home > database >  JOLT shift transformation: filter by inner value of a property (not the name)
JOLT shift transformation: filter by inner value of a property (not the name)

Time:09-05

I am trying to transform a JSON using Jolt transformation looking for some input here. I am trying to filter by the inner value of the attribute.

My goal is to get an array that contains only the items with the typeName 'xx'.

Here is my input and expected output:

Input:

{
  "id": 11,
  "item": [
    {
      "id": "11_1",
      "action": "add",
      "type": {
        "id": "11_1_xx",
        "typeName": "xx"
      },
      "item": [
        {
          "id": "11_1_1",
          "action": "add",
          "type": {
            "id": "11_1_1_zz",
            "typeName": "zz"
          },
          "item": [
            {
              "id": "11_1_1_1",
              "action": "add",
              "type": {
                "id": "11_1_1_1_xx",
                "typeName": "xx"
              }
            }
          ]
        },
        {
          "id": "11_1_2",
          "action": "add",
          "type": {
            "id": "11_1_2_xx",
            "typeName": "xx"
          },
          "item": [
            {
              "id": "11_1_2_1",
              "action": "add",
              "type": {
                "id": "11_1_2_1_zz",
                "typeName": "zz"
              }
            }
          ]
        }
      ]
    }
  ]
}

Expected output:

[
  {
    "id": "11_1",
    "action": "add",
    "type": {
      "id": "11_1_xx",
      "typeName": "xx"
    }
  },
  {
    "id": "11_1_1_1",
    "action": "add",
    "type": {
      "id": "11_1_1_1_xx",
      "typeName": "xx"
    }
}, {
    "id": "11_1_2",
    "action": "add",
    "type": {
      "id": "11_1_2_xx",
      "typeName": "xx"
    }
  }
]


Can you please help me to write a simple spec that will do this?

CodePudding user response:

Need to separate the objects by both the id and type.typeName values in the first transformation spec, in order to filter out by type.typeName=xx within the next spec long with distinctly picked id values through use of @(1,id).@(1,typeName) identifiers on the Right Hand Side for the leaf elements such as

[
  {
    "operation": "shift",
    "spec": {
      "item": {
        "*": {
          "item": {
            "*": {
              "item": {
                "*": {
                  "type": {
                    "@(1,id)": "@(1,id).@(1,typeName).id",
                    "@(1,action)": "@(1,id).@(1,typeName).action",
                    "*": "@(1,id).@(1,typeName).&1.&" // &1 replicates key name "type"
                  }
                }
              },
              "type": {
                "@(1,id)": "@(1,id).@(1,typeName).id",
                "@(1,action)": "@(1,id).@(1,typeName).action",
                "*": "@(1,id).@(1,typeName).&1.&" // & replicates the values of the elements nested within the "type" object
              }
            }
          },
          "type": {
            "@(1,id)": "@(1,id).@(1,typeName).id",
            "@(1,action)": "@(1,id).@(1,typeName).action",
            "*": "@(1,id).@(1,typeName).&1.&"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "xx": ""
      }
    }
  }
]

the demo on the site enter image description here

  • Related