Class Removr

  • All Implemented Interfaces:
    JoltTransform, SpecDriven, Transform

    public class Removr
    extends java.lang.Object
    implements SpecDriven, Transform
    Removr is a kind of JOLT transform that removes content from the input JSON.

    For comparison : Shiftr walks the input data and asks its spec "Where should this go?" Defaultr walks the spec and asks "Does this exist in the data? If not, add it." While, Removr walks the spec and asks "if this exists, remove it."

    Example : Given input JSON like

     {
       "~emVersion" : "2",
       "id":"123124",
       "productId":"31231231",
       "submissionId":"34343",
       "this" : "stays",
       "configured" : {
         "a" : "b",
         "c" : "d"
       }
     }
     
    With the desired output being :
     {
       "id":"123124",
       "this" : "stays",
    
       "configured" : {
         "a" : "b"
       }
     }
     
    This is what the Removr Spec would look like
     {
       "~emVersion" : "",
       "productId":"",
       "submissionId":"",
    
       "configured" : {
         "c" : ""
       }
     }
     
    * Removr Wildcards '*' Wildcard Valid only on the LHS ( input JSON keys ) side of a Removr Spec The '*' wildcard can be used by itself or to match part of a key. '*' wildcard by itself : To remove "all" keys under an input, use the * by itself on the LHS.
        // example input
        {
         "ratings":{
            "Set1":{
               "a":"a",
               "b":"b"
            },
            "Set2":{
                "c":"c",
                "b":"b"
            }
          },
        }
        //desired output
        {
         "ratings":{
            "Set1":{
               "a":"a"
            },
            "Set2":{
                "c":"c"
            }
          },
        }
    
        //Spec would be
        {
         "ratings":{
            "*":{
              "b":""
            },
          },
        }
        
    In this example, "Set1" and "Set2" under rating both have the same structure, and thus we can use the '*' to allow use to write more compact rules to remove "b" from all children under ratings. This is especially useful when we don't know how many children will be under ratings, but we would like to nuke certain part of it across. '*' wildcard as part of a key : This is useful for working with input JSON with keys that are "prefixed". Ex : if you had an input document like
            {
             "ratings_legacy":{
                  "Set1":{
                      "a":"a",
                      "b":"b"
                    },
                  "Set2":{
                      "a":"a",
                       "b":"b"
                   }
               }
    
             "ratings_new":{
                   "Set1":{
                       "a":"a",
                       "b":"b"
                   },
                   "Set2":{
                       "a":"a",
                       "b":"b"
                   }
              }
           }
        
    A 'rating_*' would match both keys. As in Shiftr wildcard matching, * wildcard is as non greedy as possible, which enable us to give more than one * in key. For an ouput that removed Set1 from all ratings_* key, the spec would be,
            {
             "ratings_*":{
                  "Set1":""
           }
        

    * Arrays Removr can also handle data in Arrays. It can walk thru all the elements of an array with the "*" wildcard. Additionally, it can remove individual array indicies. To do this the LHS key must be a number but in String format. Example

      "spec": {
        "array": {
          "0" : ""
        }
      }
      
    In this case, Removr will remove the zero-th item from the input "array", which will cause data at index "1" to become the new "0". Because of this, Remover matches all the literal/explicit indices first, sorts them from Biggest to Smallest, then does the removing.

    • Constructor Summary

      Constructors 
      Constructor Description
      Removr​(java.lang.Object spec)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object transform​(java.lang.Object input)
      Recursively removes data from the input JSON.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Removr

        @Inject
        public Removr​(java.lang.Object spec)
    • Method Detail

      • transform

        public java.lang.Object transform​(java.lang.Object input)
        Recursively removes data from the input JSON.
        Specified by:
        transform in interface Transform
        Parameters:
        input - the JSON object to transform in plain vanilla Jackson Map style
        Returns:
        the results of the transformation