diff --git a/src/main/java/org/embulk/filter/json_key/JsonKeyFilter.java b/src/main/java/org/embulk/filter/json_key/JsonKeyFilter.java index 390dfad..3fa7938 100644 --- a/src/main/java/org/embulk/filter/json_key/JsonKeyFilter.java +++ b/src/main/java/org/embulk/filter/json_key/JsonKeyFilter.java @@ -102,6 +102,10 @@ public JsonNode doFilter(JsonNode rootNode) // TODO: addKey is overwriting existing values. private JsonNode addKey(JsonNode node, List nestedKey, JsonNode value) { + if (node == null) { + return null; + } + if (node.isObject()) { return addKeyToObject(node, nestedKey, value); } @@ -131,7 +135,9 @@ else if (nestedKey.size() == 1) { String parentKey = nestedKey.get(0); List newNestedKey = nestedKey.subList(1, nestedKey.size()); JsonNode newNode = addKey(object.get(parentKey), newNestedKey, value); - object.set(parentKey, newNode); + if (newNode != null) { + object.set(parentKey, newNode); + } return object; } } @@ -156,7 +162,9 @@ else if (nestedKey.size() == 1) { if (NumberUtils.isNumber(parentIdx)) { int idx = Integer.parseInt(parentIdx); JsonNode newNode = addKey(object.get(idx), newNestedKey, value); - object.set(idx, newNode); + if (newNode != null) { + object.set(idx, newNode); + } } return object; } @@ -184,6 +192,10 @@ public JsonNode doFilter(JsonNode rootNode) // if NullNode has come before end of a nested key, return NullNode not EmptyObjectNode. private JsonNode dropKey(JsonNode node, List nestedKey) { + if (node == null) { + return null; + } + if (node.isObject()) { return dropKeyFromObject(node, nestedKey); } @@ -209,7 +221,9 @@ else if (nestedKey.size() == 1) { String parentKey = nestedKey.get(0); List newNestedKey = nestedKey.subList(1, nestedKey.size()); JsonNode newNode = dropKey(object.get(parentKey), newNestedKey); - object.set(parentKey, newNode); + if (newNode != null) { + object.set(parentKey, newNode); + } return object; } } @@ -233,7 +247,9 @@ else if (nestedKey.size() == 1) { if (NumberUtils.isNumber(parentIdx)) { int idx = Integer.parseInt(parentIdx); JsonNode newNode = dropKey(object.get(idx), newNestedKey); - object.set(idx, newNode); + if (newNode != null) { + object.set(idx, newNode); + } } return object; }