Search code examples
jsonapache-nifijolt

Move field from an json object to another object with condition


I have the following JSON structure :

{
  "1": {
    "precedingId": "0",
    "value": "A"
  },
  "2": {
    "precedingId": "1",
    "value": "B"
  },
  "3": {
    "precedingId": "2",
    "value": "C"
  }
}

I'm trying to get the following output using Jolt :

{
  "1": {
    "precedingId": "0",
    "precedingValue" : null, // null because there is no object with key equals to 0
    "value": "A"
  },
  "2": {
    "precedingId": "1",
    "precedingValue" : "A",
    "value": "B"
  },
  "3": {
    "precedingId": "2",
    "precedingValue" : "B"
    "value": "C"
  }
}

For each object, take precedingId as a key and lookup 1 level above if there is any corresponding object and if so then take value and put it in current object as precedingValue.

I tried several specs for few days but I think it mights be not possible doing this kind of logic in Jolt ?


Solution

  • You can use the following transformation spec :

    [
      {//group the attributes by inner "precedingId" values of each object
       //by @1,precedingId prefixes
       //except the precedingValues from the other objects    
       //in order to transfer them from their original object
       //by using &1 prefix 
        "operation": "shift",
        "spec": {
          "*": {
            "$": "@1,precedingId.id",
            "value": ["@1,precedingId.value", "&1.precedingValue"],
            "precedingId": "@1,precedingId.&"
          }
        }
      },
      {//this time group the attributes by the generated "id" values
        "operation": "shift",
        "spec": {
          "*": {
            "id": { "": "" }, //get rid of temporarily generated "id" values
            "*": "@1,id.&"
          }
        }
      },
      {//set the missing value
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "~precedingValue": null// ~ operator means if this attribute does not exist or is null, then make it be null
          }
        }
      }
    ]
    

    the demo on the site Jolt Transform Demo Using v0.1.1 is :

    enter image description here