jsonconditional-statementsjolt

Conditional check to clear/empty field using Jolt on multiple types of field values


I have the below JSON

[
  {
    "field1": "xyz",
    "field2": "mno",
    "res1": "pqrs"
  },
  {
    "field1": "xyz1",
    "field2": "mno1",
    "res1": "pqrs1"
    
  }..other entries
]

And the below jolt spec works fine to empty the field but I need to check if the value of field1 is /folder1/subfolder/name or /folder2/subfolder/name or /folder3/subfolder/name before emptying the res1

[
  {
    "operation": "default",
    "spec": {
      "*": {
        "*": {
          "res": ""
        }
      }
    }
  }
]

Need help in updating the above for conditional check in Jolt Spec

Edited --I need to like this (this does not work)

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "if": "field1 == '/abc/bvcd' || field1 == '/xyz/prq/'",
        "then": {
          "res1": ""
        },
        "else": {}
      }
    }
  }
]

Solution

  • You can use the following spec :

    [
      { //separate the objects by indexes and "field1" values
        //considering the case of "field1" values to be identical 
        //for different objects
        "operation": "shift",
        "spec": {
          "*": {
            "*": "&1.@1,field1.&"
          }
        }
      },
      {//empty "res1" attributes only as long as the matching occurs
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "/folder1/subfolder/name?|/folder2/subfolder/name?|/folder3/subfolder/name?": {
              "res1": ""
            }
          }
        }
      },
      { //get rid of the outer keys
        "operation": "shift",
        "spec": {
          "*": {
            "*": "[]"
          }
        }
      }
    ]
    

    where ? operator leads to conditional matching.

    If any of those object keys exists, then "res1":"" match occurs for that individual one.

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

    enter image description here