Search code examples
jsontransformationjolt

[Jolt]remove array element based on a dynamic value provided in key/value format


I am trying to remove an array element if the element is value of a fixed key. Input:

{
  "components": {
    "schemas": {
      "element1": {
        "type": "string",
        "remove_element_from_values": ["value_3"],
        "enum": [
          "value_1",
          "value_2",
          "value_3",
          "value_4",
          "value_5"
        ],
        "example": "example of element1"
      },
      "element2": {
        "type": "string",
        "remove_element_from_values": ["value_1", "value_5"],
        "values": [
          "value_1",
          "value_2",
          "value_3",
          "value_4",
          "value_5"
        ],
        "example": "example of element2"
      }
    }
  }
}

I need a jolt spec which will look into "remove_element_from_values" field and based on the input array there it will remove the value(s) from the values array elements and also removes the "remove_element_from_values" from final output.

Expected output:

{
  "components": {
    "schemas": {
      "element1": {
        "type": "string",
        "enum": [
          "value_1",
          "value_2",
          "value_4",
          "value_5"
        ],
        "example": "example of element1"
      },
      "element2": {
        "type": "string",
        "enum": [
          "value_2",
          "value_3",
          "value_4"
        ],
        "example": "example of element2"
      }
    }
  }
}

any help will be greatly appreciated. I am trying to get a working spec for this for sometime now.

I tried looking into almost all different wiki/issues available in jolt space but not able to find my solution.


Solution

  • You can use the following shift transformation spec

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "*alues|enum": {
                  "*": {
                    "$": "&5.&4.&3.enum.@0"
                  }
                },
                "type|example": "&3.&2.&1.&"
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "enum": {
                  "*": {
                    "$": "&5.&4.&3.&2.@0"
                  }
                },
                "t*|e*": "&3.&2.&1.&"
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": {
                "enum": {
                  "*": {
                    "@": "&5.&4.&3.&2"
                  }
                },
                "*": "&3.&2.&1.&"
              }
            }
          }
        }
      }
    ]
    

    where matching values generate arrays in the first spec, they will vanish after key-value exchange in the second spec

    the demo on the site http://jolt-demo.appspot.com/ is :

    enter image description here