@Test public void givenJsonWithEmptyElementsThenReturnEmptyJsonString() throws RemoveEmptyElementException { jsonStringInput = "{'remove1': '','remove2': ''}"; expectedJsonStringOutput = "{}"; actualJsonStringOutput = jsonServiceUnderTest.removeEmptyElementsJson(jsonStringInput); assertEquals(expectedJsonStringOutput, actualJsonStringOutput); }
public String removeEmptyElementsJson(String json) throws RemoveEmptyElementException { String normalizedJson = json.trim(); char wrappingQuote = retrieveWrappingQuoteTypeOfJsonMemberNames(normalizedJson); Map<String, Object> jsonMap; try { parseJsonForInconsistencies(normalizedJson); Object jsonElement = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(normalizedJson); if(jsonElement instanceof JSONObject) { jsonMap = JsonPath.read(normalizedJson, "$"); removeEmptyElementsFromMap(jsonMap); return generateResultingJsonString(wrappingQuote, jsonMap); } else if(jsonElement instanceof JSONArray) { JSONArray jsonArray = (JSONArray) jsonElement; Map<String, Object> tmpMap = new LinkedHashMap<>(); tmpMap.put("", jsonArray); JSONObject jsonObject = new JSONObject(tmpMap); jsonMap = JsonPath.read(jsonObject.toJSONString(), "$"); removeEmptyElementsFromMap(jsonMap); return jsonMap.isEmpty() ? generateResultingJsonString(wrappingQuote, new JSONArray()) : generateResultingJsonString(wrappingQuote, (JSONArray)jsonMap.get("")); } else { throw new RemoveEmptyElementException(ExceptionMsgs.JSON_OBJECT_SHOULD_BE_OBJECT_OR_ARRAY); } } catch (InvalidJsonException | JsonSyntaxException | ParseException ije) { throw new RemoveEmptyElementException(ije); } }
JsonService { public String removeEmptyElementsJson(String json) throws RemoveEmptyElementException { String normalizedJson = json.trim(); char wrappingQuote = retrieveWrappingQuoteTypeOfJsonMemberNames(normalizedJson); Map<String, Object> jsonMap; try { parseJsonForInconsistencies(normalizedJson); Object jsonElement = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(normalizedJson); if(jsonElement instanceof JSONObject) { jsonMap = JsonPath.read(normalizedJson, "$"); removeEmptyElementsFromMap(jsonMap); return generateResultingJsonString(wrappingQuote, jsonMap); } else if(jsonElement instanceof JSONArray) { JSONArray jsonArray = (JSONArray) jsonElement; Map<String, Object> tmpMap = new LinkedHashMap<>(); tmpMap.put("", jsonArray); JSONObject jsonObject = new JSONObject(tmpMap); jsonMap = JsonPath.read(jsonObject.toJSONString(), "$"); removeEmptyElementsFromMap(jsonMap); return jsonMap.isEmpty() ? generateResultingJsonString(wrappingQuote, new JSONArray()) : generateResultingJsonString(wrappingQuote, (JSONArray)jsonMap.get("")); } else { throw new RemoveEmptyElementException(ExceptionMsgs.JSON_OBJECT_SHOULD_BE_OBJECT_OR_ARRAY); } } catch (InvalidJsonException | JsonSyntaxException | ParseException ije) { throw new RemoveEmptyElementException(ije); } } }
JsonService { public String removeEmptyElementsJson(String json) throws RemoveEmptyElementException { String normalizedJson = json.trim(); char wrappingQuote = retrieveWrappingQuoteTypeOfJsonMemberNames(normalizedJson); Map<String, Object> jsonMap; try { parseJsonForInconsistencies(normalizedJson); Object jsonElement = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(normalizedJson); if(jsonElement instanceof JSONObject) { jsonMap = JsonPath.read(normalizedJson, "$"); removeEmptyElementsFromMap(jsonMap); return generateResultingJsonString(wrappingQuote, jsonMap); } else if(jsonElement instanceof JSONArray) { JSONArray jsonArray = (JSONArray) jsonElement; Map<String, Object> tmpMap = new LinkedHashMap<>(); tmpMap.put("", jsonArray); JSONObject jsonObject = new JSONObject(tmpMap); jsonMap = JsonPath.read(jsonObject.toJSONString(), "$"); removeEmptyElementsFromMap(jsonMap); return jsonMap.isEmpty() ? generateResultingJsonString(wrappingQuote, new JSONArray()) : generateResultingJsonString(wrappingQuote, (JSONArray)jsonMap.get("")); } else { throw new RemoveEmptyElementException(ExceptionMsgs.JSON_OBJECT_SHOULD_BE_OBJECT_OR_ARRAY); } } catch (InvalidJsonException | JsonSyntaxException | ParseException ije) { throw new RemoveEmptyElementException(ije); } }  }
JsonService { public String removeEmptyElementsJson(String json) throws RemoveEmptyElementException { String normalizedJson = json.trim(); char wrappingQuote = retrieveWrappingQuoteTypeOfJsonMemberNames(normalizedJson); Map<String, Object> jsonMap; try { parseJsonForInconsistencies(normalizedJson); Object jsonElement = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(normalizedJson); if(jsonElement instanceof JSONObject) { jsonMap = JsonPath.read(normalizedJson, "$"); removeEmptyElementsFromMap(jsonMap); return generateResultingJsonString(wrappingQuote, jsonMap); } else if(jsonElement instanceof JSONArray) { JSONArray jsonArray = (JSONArray) jsonElement; Map<String, Object> tmpMap = new LinkedHashMap<>(); tmpMap.put("", jsonArray); JSONObject jsonObject = new JSONObject(tmpMap); jsonMap = JsonPath.read(jsonObject.toJSONString(), "$"); removeEmptyElementsFromMap(jsonMap); return jsonMap.isEmpty() ? generateResultingJsonString(wrappingQuote, new JSONArray()) : generateResultingJsonString(wrappingQuote, (JSONArray)jsonMap.get("")); } else { throw new RemoveEmptyElementException(ExceptionMsgs.JSON_OBJECT_SHOULD_BE_OBJECT_OR_ARRAY); } } catch (InvalidJsonException | JsonSyntaxException | ParseException ije) { throw new RemoveEmptyElementException(ije); } }  @NotNull static JsonNode evaluateJsonPathQuery(@Nullable final String jsonObject, @Nullable final String jsonPath); String removeEmptyElementsJson(String json); }
JsonService { public String removeEmptyElementsJson(String json) throws RemoveEmptyElementException { String normalizedJson = json.trim(); char wrappingQuote = retrieveWrappingQuoteTypeOfJsonMemberNames(normalizedJson); Map<String, Object> jsonMap; try { parseJsonForInconsistencies(normalizedJson); Object jsonElement = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(normalizedJson); if(jsonElement instanceof JSONObject) { jsonMap = JsonPath.read(normalizedJson, "$"); removeEmptyElementsFromMap(jsonMap); return generateResultingJsonString(wrappingQuote, jsonMap); } else if(jsonElement instanceof JSONArray) { JSONArray jsonArray = (JSONArray) jsonElement; Map<String, Object> tmpMap = new LinkedHashMap<>(); tmpMap.put("", jsonArray); JSONObject jsonObject = new JSONObject(tmpMap); jsonMap = JsonPath.read(jsonObject.toJSONString(), "$"); removeEmptyElementsFromMap(jsonMap); return jsonMap.isEmpty() ? generateResultingJsonString(wrappingQuote, new JSONArray()) : generateResultingJsonString(wrappingQuote, (JSONArray)jsonMap.get("")); } else { throw new RemoveEmptyElementException(ExceptionMsgs.JSON_OBJECT_SHOULD_BE_OBJECT_OR_ARRAY); } } catch (InvalidJsonException | JsonSyntaxException | ParseException ije) { throw new RemoveEmptyElementException(ije); } }  @NotNull static JsonNode evaluateJsonPathQuery(@Nullable final String jsonObject, @Nullable final String jsonPath); String removeEmptyElementsJson(String json);  }