@Test public void an_array_criteria_with_multiple_results_can_be_deleted(){ InputStream stream = this.getClass().getResourceAsStream("/json_array_multiple_delete.json"); String deletePath = "$._embedded.mandates[?(@.count=~/0/)]"; DocumentContext documentContext = JsonPath.parse(stream); documentContext.delete(deletePath); List<Object> result = documentContext.read(deletePath); assertThat(result.size()).isEqualTo(0); }
@Test public void an_array_criteria_can_be_deleted() { List<String> res = parse(JSON_DOCUMENT) .delete("$.store.book[?(@.category == 'fiction')]") .read("$.store.book[*].category"); assertThat(res).containsExactly("reference"); }
@Test public void multi_prop_delete() { List<Map<String, Object>> res = parse(JSON_DOCUMENT).delete("$.store.book[*]['author', 'category']").read("$.store.book[*]['author', 'category']"); assertThat(res).containsExactly(EMPTY_MAP, EMPTY_MAP, EMPTY_MAP, EMPTY_MAP); }
@Test public void a_path_can_be_deleted() { Object o = parse(JSON_DOCUMENT).delete("$.store.book[*].display-price").json(); List<Integer> result = parse(o).read("$.store.book[*].display-price"); assertThat(result).isEmpty(); }
@Test public void operations_can_chained() { Object o = parse(JSON_DOCUMENT) .delete("$.store.book[*].display-price") .set("$.store.book[*].category", "A") .json(); List<Integer> prices = parse(o).read("$.store.book[*].display-price"); List<String> categories = parse(o).read("$.store.book[*].category"); assertThat(prices).isEmpty(); assertThat(categories).containsExactly("A", "A", "A", "A"); }
@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); assertThat(categories).containsOnly("fiction"); }
public TestSuite(Map<String, Object> suiteJson, DocumentContext doc) { super(suiteJson); this.tests = new ArrayList<>(); this.timeOut = (int) suiteJson.get("timeout"); this.parallel = (boolean) suiteJson.get("parallel"); List<String> testIds = (List) suiteJson.get("tests"); for (String testId : testIds) { String jsonpath = "$.tests[?(@.id == '" + testId.trim() + "')]"; List<Map<String, Object>> testCases = doc.read(jsonpath); this.tests.add(new TestCase(testCases.get(0))); doc.delete(jsonpath); // one way to mark parsed tests } }
/** * Deletes JSON nodes at the given path. * @param path The JSON path * @param filters Optional JSON path filters * @return The new JSON fixture */ public JsonFixture delete(final String path, final Predicate... filters) { return new JsonFixture(cloneContext().delete(JsonPath.compile(path, filters))); }
doc.delete(path); } else { if (right.startsWith("[")) {
/** * Check that two json representation are equals. * * @param actual Actual representation. * @param expected Expected representation. * @param entries Name of entries to ignore. * @return Assertion result. */ public AssertionResult isEqualToIgnoring(String actual, String expected, Iterable<String> entries) { final String actualJson; final String expectedJson; // Check if some keys needs to be ignored if (entries.iterator().hasNext()) { DocumentContext actualCtx = JsonPath.parse(actual); DocumentContext expectedCtx = JsonPath.parse(expected); // Remove keys to ignore for (String entry : entries) { String path = toJsonPath(entry); actualCtx.delete(path); expectedCtx.delete(path); } actualJson = actualCtx.jsonString(); expectedJson = expectedCtx.jsonString(); } else { actualJson = actual; expectedJson = expected; } return doComparison(actualJson, expectedJson); }
@Override public void visitPipelineTemplate(PipelineTemplate pipelineTemplate) { pipelineTemplate.getStages() .stream() .filter(s -> s.getInheritanceControl() != null) .forEach(s -> { DocumentContext dc = JsonPath.parse(s.getConfig()); s.getInheritanceControl().getMerge().forEach(r -> merge(dc, r)); s.getInheritanceControl().getReplace().forEach(r -> replace(dc, r)); s.getInheritanceControl().getRemove().forEach(r -> dc.delete(r.getPath())); }); }
if (optional) { if (forMatch || sv.isNull()) { root.delete(path); } else if (!sv.isJsonLike()) {
if (!clone) { String modifiedJsonString = JsonPath.parse(jsonString).delete(jsonPath).jsonString(); try { JsonUtil.getNewJsonPayload(context, modifiedJsonString, true, true);