Home > Software design >  Jolt nested array transformation
Jolt nested array transformation

Time:11-01

I'm a beginner in jolt transformations and I'm having below input payload that needs to be transformed before sending out

  1. i want to create separate node based on nested array values and insert it back into array and remove the nested array completely from the output
{
  "Def": {
    "capacity": {
      "accounttype": {
        "value": "customer"
      },
      "customer": {
        "name": "abc"
      },
      "config": [
        {
          "restriction": {
            "value": "inbound"
          },
          "serviceConfig": [
            {
              "serviceType": {
                "value": "standard"
              }
            },
            {
              "serviceType": {
                "value": "special"
              }
            }
          ]
        }
      ]
    }
  }
}

and expected output is below

{
  "Def": {
    "capacity": {
      "accounttype": {
        "value": "customer"
      },
      "customer": {
        "name": "abc"
      },
      "config": [
        {
          "restriction": {
            "value": "inbound"
          },
          "serviceType": {
            "value": "standard"
          }
        },
        {
          "restriction": {
            "value": "inbound"
          },
          "serviceType": {
            "value": "special"
          }
        }
      ]
    }
  }
}

I did try multiple things but none of them seems to work,

CodePudding user response:

You can use a shift transformation spec in which walk through the indexes of serviceConfig array and then tile the other elements by using "*": "&2.&1.&" (as else case) such as

[
  {
    "operation": "shift",
    "spec": {
      "Def": {
        "capacity": {
          "*": "&2.&1.&", // &2 stands for going the tree 2 levels up and grabbing the value "Def", &1 for "capacity", & as leaf node which copies the current node's value
          "config": {
            "*": {
              "serviceConfig": {
                "*": {
                  "@(2,restriction)": "&6.&5.&4[&1].restriction", 
                  "*": "&6.&5.&4[&1].&" // &6 -> "Def", &5 -> "capacity", &4 -> "config", [&1] -> indexes of serviceConfig, & -> leaf node which copies the current node's value
                }
              }
            }
          }
        }
      }
    }
  }
]

the demo on the site enter image description here

  • Related