1

I want to remove blob columns from JSON objects. I need to check if any of the object has "@type": "blob", the entire column should be dropped. Ex. following is a record from a DB. 'experience', 'hitpoints', 'name', 'uuid', 'image' (optional) are the columns. since the record has a blob column i.e image. It should be dropped.

Sample I/P:

  {
      "experience": 14248,
      "hitpoints": 9223372036854775807,
      "name": "Aaron1",
      "uuid": "78edf902-7dd2-49a4-99b4-1c94ee286a33",
      "image": {
        "@type": "blob",
        "content_type": "image/jpeg",
        "digest": "sha1–4xlj1AKFgLdzcD7a1pVChrVTJIc=",
        "length": 3888349
      }
    },
    {
     "experience": 14252,
     "hitpoints": 92233720368512345,
     "name": "Aaron2",
     "uuid": "78edf902-7dd2-49a4-99b4-1a94ff286a45",
    }

Sample O/P:

{
  "experience": 14248,
  "hitpoints": 9223372036854775807,
  "name": "Aaron1",
  "uuid": "78edf902-7dd2-49a4-99b4-1c94ee286a33",
},
{
  "experience": 14252,
  "hitpoints": 92233720368512345,
  "name": "Aaron2",
  "uuid": "78edf902-7dd2-49a4-99b4-1a94ff286a45",
 }

Is there a way to achieve this by using optimized JSON parsing. Currently, my logic follows the steps:

  1. I'm parsing through the entire object using a function where I'm looping through the node to read the object.
  2. Calling the 'blobChecker' function on every object.
  3. Assigning null to the node if it contains blob.
  4. Skipping the null node in the original function that invokes 'blobChecker'

The original function to parseJSON:

parseJsonNode(JsonNode node){
blobNodeChecker(node);
if(node!=null)
//The funtionality
}

The blobNodeChecker Function:

blobNodeChecker(JsonNode node) {
Boolean isBlob = false;
String blobNode = null;
Iterator<Map.Entry<String, JsonNode>> fields = node.fields();
while (fields.hasNext()) {
    Map.Entry<String, JsonNode> next = fields.next();
    String key = next.getKey();
    String val = next.getValue().toString().toLowerCase();
    if (key.equals("@type")) {
        if (val.contains("blob")) {
            isBlob = true;
            break;
    }
    }
}
if (isBlob) {
    node = null;
}
return node;
}
Siena
  • 778
  • 10
  • 23

1 Answers1

0

How about something like below. You can directly read a path and depending upon that delete a node. No need to loop all keys.

String tt = " {" + 
            "      \"experience\": 14248," + 
            "      \"hitpoints\": 9223372036854775807," + 
            "      \"name\": \"Aaron1\"," + 
            "      \"uuid\": \"78edf902-7dd2-49a4-99b4-1c94ee286a33\"," + 
            "      \"image\": {" + 
            "        \"@type\": \"blob\"," + 
            "        \"content_type\": \"image/jpeg\"," + 
            "        \"digest\": \"sha1–4xlj1AKFgLdzcD7a1pVChrVTJIc=\"," + 
            "        \"length\": 3888349" + 
            "      }" + 
            "    }";
    ObjectMapper mapper = new ObjectMapper();
         mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
         JsonFactory factory = mapper.getFactory();
         JsonParser createParser = factory.createParser(tt);
         JsonNode actualObj1 = mapper.readTree(createParser);
         JsonNode path = actualObj1.path("image").path("@type");
         if( path != null && "blob".equalsIgnoreCase(path.asText())) {
             ((ObjectNode)actualObj1).remove("image");
         }
         System.out.println(actualObj1.toString());
A Paul
  • 8,113
  • 3
  • 31
  • 61