Home > Software engineering >  Jolt spec to transform fields in selective objects
Jolt spec to transform fields in selective objects

Time:12-20

For my input json, I need to write a Jolt spec that can modify manager attribute by joining the value & position fields in only the user type object and not the other object type account. The input could be a list of such objects.

Input:

[
  {
    "user": {
      "userName": "Mike",
      "manager": {
        "value": "Harvey",
        "position": "Director"
      }
    },
    "account": {
      "userLogin": "Mike1987",
      "status": true
    }
  },
  {
    "user": {
      "userName": "Alex",
      "manager": {
        "value": "Daniel",
        "position": "President"
      }
    },
    "account": {
      "userLogin": "Alex12a",
      "status": true
    }
  }
]

Desired output:

[
  {
    "user": {
      "userName": "Mike",
      "managerRef": {
        "info": "Harvey_Director"
      }
    },
    "account": {
      "userLogin": "Mike1987",
      "status": true
    }
  },
  {
    "user": {
      "userName": "Alex",
      "managerRef": {
        "info": "Daniel_President"
      }
    },
    "account": {
      "userLogin": "Alex12a",
      "status": true
    }
  }
]

Could someone please help in creating the spec for this transformation. Thanks.

CodePudding user response:

You can use a modify transformation spec which contains a concat function such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "user": {
          "manager": {
            "info": "=concat(@(1,value),'_',@(1,position))"
          }
        }
      }
    }
  },
  {
  // get rid of existing attributes within the object
    "operation": "remove",
    "spec": {
      "*": {
        "u*": {
          "m*": {
            "p*|v*": ""
          }
        }
      }
    }
  }
]

the demo on the site enter image description here

An alternative method which includes a shift transformation as desired, based on the comment would be

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "user": {
          "*": "[&2].&1.&",
          "manager": {
            "*": "[&3].&2.&1.info"
          }
        },
        "*": "[&1].&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "user": {
          "manager": {
            "info": "=join('_',@(1,&))"
          }
        }
      }
    }
  }
]

the demo for this case would be :

enter image description here

  • Related