Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jolt conditional spec

Tags:

java

json

jolt

I want a conditional transformation where I need to add a property in output if the value of a specific field in input matches my condition. Below is my input and output required.


Input

{
    "attr": [
        {
            "name": "first",
            "validations": [
                {
                    "type": "Required",
                    "value": true
                }
            ]
        },
        {
            "name": "last",
            "validations": [
                {
                    "type": "lenght",
                    "value": "10"
                }
            ]
        },
        {
            "name": "email",
            "validations": [
                {
                    "type": "min",
                    "value": 10
                }
            ]
        }
    ]
}

Output

{
  "out" : [
    {
      "name" : "first",
      "required" : "yes"
    },{
      "name" : "last"
    },{
      "name" : "email"
    }
  ]
}

So I am able to get till the condition, but inside condition, & and @ are being respective to the input rather than to the output. Can anybody help me out with the transformation. Below is the spec i have written so far.

[
  {
    "operation": "shift",
    "spec": {
      "attr": {
        "*": {
          "name": "out.&1.name",
          "validations": {
            "*": {
              "type": {
                "Required": {
                  "@(2,value)": "out.&1.req"
                }
              }
            }
          }
        }
      }
    }
  }
]
like image 813
yeswanth Avatar asked Apr 21 '16 14:04

yeswanth


1 Answers

This spec does the transform.

[
  {
    "operation": "shift",
    "spec": {
      "attr": {
        "*": {
          "name": "out[&1].name",
          "validations": {
            "*": {
              "type": {
                "Required": {
                  "#yes": "out[&5].required"
                }
              }
            }
          }
        }
      }
    }
  }
]

However, I think you meant to grab the "value" : true that is a sibling of the "Required" : true, rather than have the output be "yes".

If so swap in this bit.

"Required": {
 "@(2,value)": "out[&5].required"
}
like image 79
Milo S Avatar answered Nov 14 '22 15:11

Milo S