Home > Blockchain >  Java Jolt copy properties with condition
Java Jolt copy properties with condition

Time:08-03

I would like to transform flat data to array with conditional value of properties:

My input:

{
  "data": {
    "myProp": true,
    "myAnother": true,
    "myAnotherOne": false
  }
}

My desired output:

{
  "values": ["MY_PROP", "MY_ANOTHER"]
}

I think to use shift operation in order to normalize the name camelCase to ENUM standard. I need to put in array only properties that are true in their value.

I tried many different options with no success.

What is the best way to do that?

CodePudding user response:

Yes, you can use a shift transformation spec along with matching the key name true with $1 wildcard coming from the inner object to provide the conditional logic such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "true": {
            "$1": "values"
          }
        }
      }
    }
  }
]

the demo on the site enter image description here

If the style(converting the componenets of the array to uppercase and adding an underscore) matters, then use a prolonged version of the spec like

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "true": {
            "$1": "val"
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=toUpper(@(1,&))"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=split('MY', @(1,&))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "1": "&"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "#MY_": "&1",
          "@": "&"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=join('', @(1,&))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "values[]"
    }
  }
]
  • Related