Jolt spec for a matching math condition

57 Views Asked by At

I am trying to fill a "completed" field if the "total" and the "current" fields match and i can´t figure the spec for it.

Being my original JSON:

{
  "data": {
    "aField": {
      "current": 5
    },
    "otherField": {
      "total": 5
    }
  }
}

I´m expecting a result like:

{
  "status": "completed"
}

In case I receive any other JSON where the current and total don´t match I would like another output like:

{
  "data": {
    "aField": {
      "current": 4
    },
    "otherField": {
      "total": 5
    }
  }
}

I´m expecting a result like:

{
  "status": "not completed"
}

And in case there is no current and/or total fields, the expected result will be:

{
  "status": "not found"
}
1

There are 1 best solutions below

0
On

You can use the following transformation :

[

  {
    "operation": "shift",
    "spec": {
      "data": {
        "@aField.current": "bothExist[]",
        "@otherField.total": "bothExist[]",
        "#a": "equal.@(1,aField.current)",
        "#b": "equal.@(1,otherField.total)",
        "#nothing": "fakeItem" // just in order to persist a non-null 
          // JSON value for the case non of the
          // elements exist
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "bothExist|equal": ["=size", 0], // set to zero whenever buth of the elements don't exist
      "b_e": "=concat(@(1,bothExist),'_',@(1,equal))" // preparation of the identifier 
                                                      // for the upcoming conditional
    }
  },
  {
    "operation": "shift",
    "spec": {
      "b_e": {
        "2_1": { "#completed": "status" },
        "2_2": { "#not completed": "status" },
        "*": { "#not found": "status" } // if at least one of those two elements doesn't exist 
      }
    }
  }
]