Example #1
0
  @Test
  public void issue_97() throws Exception {
    String json =
        "{ \"books\": [ "
            + "{ \"category\": \"fiction\" }, "
            + "{ \"category\": \"reference\" }, "
            + "{ \"category\": \"fiction\" }, "
            + "{ \"category\": \"fiction\" }, "
            + "{ \"category\": \"reference\" }, "
            + "{ \"category\": \"fiction\" }, "
            + "{ \"category\": \"reference\" }, "
            + "{ \"category\": \"reference\" }, "
            + "{ \"category\": \"reference\" }, "
            + "{ \"category\": \"reference\" }, "
            + "{ \"category\": \"reference\" } ]  }";

    Configuration conf =
        Configuration.builder()
            .jsonProvider(new GsonJsonProvider())
            .mappingProvider(new GsonMappingProvider())
            .build();

    DocumentContext context = JsonPath.using(conf).parse(json);
    context.delete("$.books[?(@.category == 'reference')]");

    List<String> categories = context.read("$..category", List.class);

    Assertions.assertThat(categories).containsOnly("fiction");
  }
 /**
  * Removes the fields with empty obibaFiles from content.
  *
  * @param newPaths
  * @param newContext
  */
 private void cleanup(Map<String, JSONArray> newPaths, DocumentContext newContext) {
   newPaths
       .keySet()
       .stream()
       .forEach(
           p -> {
             if (newPaths.get(p).isEmpty()) {
               newContext.delete(p.replace("['obibaFiles']", ""));
             }
           });
 }