CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-jayway-jsonpath--json-path-assert

A library with Hamcrest matchers for JsonPath - provides assertion capabilities for JSON documents using JsonPath expressions

Pending
Overview
Eval results
Files

jsonpath-matchers.mddocs/

JsonPath Matchers

Core Hamcrest matchers for JsonPath-based assertions, providing the primary API for validating JSON documents using path expressions. These matchers integrate JsonPath's powerful querying capabilities with Hamcrest's expressive assertion framework.

Capabilities

Path Existence Validation

Validates whether specified JsonPath expressions exist within JSON documents.

/**
 * Validates that the JSON document contains the specified path
 * @param jsonPath JsonPath expression as string
 * @return Matcher that succeeds if path exists
 */
public static Matcher<? super Object> hasJsonPath(String jsonPath);

/**
 * Validates that the JSON document contains the specified path and the value matches the given matcher
 * @param jsonPath JsonPath expression as string
 * @param resultMatcher Hamcrest matcher to validate the path value
 * @return Matcher that succeeds if path exists and value matches
 */
public static <T> Matcher<? super Object> hasJsonPath(String jsonPath, Matcher<T> resultMatcher);

/**
 * Validates that the JSON document does NOT contain the specified path
 * @param jsonPath JsonPath expression as string
 * @return Matcher that succeeds if path does not exist
 */
public static Matcher<? super Object> hasNoJsonPath(String jsonPath);

Usage Examples:

import static com.jayway.jsonpath.matchers.JsonPathMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

String json = "{\"user\":{\"name\":\"Alice\",\"age\":30},\"items\":[\"book\",\"pen\"]}";

// Basic existence checks
assertThat(json, hasJsonPath("$.user.name"));
assertThat(json, hasJsonPath("$.items[0]"));
assertThat(json, hasNoJsonPath("$.user.email"));

// Value validation
assertThat(json, hasJsonPath("$.user.name", equalTo("Alice")));
assertThat(json, hasJsonPath("$.user.age", equalTo(30)));
assertThat(json, hasJsonPath("$.items", hasSize(2)));
assertThat(json, hasJsonPath("$.items[*]", hasItem("book")));

JSON Validity Checks

Validates that input data is valid JSON and optionally applies additional matchers to the parsed content.

/**
 * Validates that the input is valid JSON (contains Map or List at root)
 * @return Matcher that succeeds for valid JSON documents
 */
public static Matcher<Object> isJson();

/**
 * Validates that the input is valid JSON and applies the given matcher to the ReadContext
 * @param matcher Matcher to apply to the parsed JSON ReadContext
 * @return Matcher that succeeds if JSON is valid and matcher succeeds
 */
public static Matcher<Object> isJson(Matcher<? super ReadContext> matcher);

/**
 * Type-specific JSON validation for String inputs
 * @param matcher Matcher to apply to the parsed JSON ReadContext
 * @return Matcher specific to String inputs
 */
public static Matcher<String> isJsonString(Matcher<? super ReadContext> matcher);

/**
 * Type-specific JSON validation for File inputs
 * @param matcher Matcher to apply to the parsed JSON ReadContext
 * @return Matcher specific to File inputs
 */
public static Matcher<File> isJsonFile(Matcher<? super ReadContext> matcher);

Usage Examples:

String validJson = "{\"key\":\"value\"}";
String invalidJson = "{ invalid-json }";
File jsonFile = new File("data.json");

// Basic validity
assertThat(validJson, isJson());
assertThat(invalidJson, not(isJson()));

// Type-specific validation with additional checks
assertThat(validJson, isJsonString(withJsonPath("$.key")));
assertThat(jsonFile, isJsonFile(withJsonPath("$.users[*]", hasSize(greaterThan(0)))));

// Complex validation combining multiple conditions
assertThat(validJson, isJson(allOf(
    withJsonPath("$.key", equalTo("value")),
    withoutJsonPath("$.missing")
)));

ReadContext Matchers for Path Evaluation

Low-level matchers that work directly with JsonPath's ReadContext, enabling complex matcher combinations and pre-compiled path support.

/**
 * Creates matcher for ReadContext with specified path (with optional filters)
 * @param jsonPath JsonPath expression as string
 * @param filters Optional Predicate filters for path evaluation
 * @return Matcher for ReadContext that succeeds if path exists
 */
public static Matcher<? super ReadContext> withJsonPath(String jsonPath, Predicate... filters);

/**
 * Creates matcher for ReadContext with pre-compiled JsonPath
 * @param jsonPath Pre-compiled JsonPath instance
 * @return Matcher for ReadContext that succeeds if path exists
 */
public static Matcher<? super ReadContext> withJsonPath(JsonPath jsonPath);

/**
 * Creates matcher for ReadContext with path and value validation
 * @param jsonPath JsonPath expression as string
 * @param resultMatcher Matcher to validate the path value
 * @return Matcher that succeeds if path exists and value matches
 */
public static <T> Matcher<? super ReadContext> withJsonPath(String jsonPath, Matcher<T> resultMatcher);

/**
 * Creates matcher for ReadContext with pre-compiled path and value validation
 * @param jsonPath Pre-compiled JsonPath instance
 * @param resultMatcher Matcher to validate the path value
 * @return Matcher that succeeds if path exists and value matches
 */
public static <T> Matcher<? super ReadContext> withJsonPath(JsonPath jsonPath, Matcher<T> resultMatcher);

/**
 * Creates matcher for ReadContext that succeeds when path does NOT exist
 * @param jsonPath JsonPath expression as string
 * @param filters Optional Predicate filters for path evaluation
 * @return Matcher that succeeds if path does not exist
 */
public static Matcher<? super ReadContext> withoutJsonPath(String jsonPath, Predicate... filters);

/**
 * Creates matcher for ReadContext that succeeds when pre-compiled path does NOT exist
 * @param jsonPath Pre-compiled JsonPath instance
 * @return Matcher that succeeds if path does not exist
 */
public static Matcher<? super ReadContext> withoutJsonPath(JsonPath jsonPath);

Usage Examples:

import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Predicate;
import static com.jayway.jsonpath.Criteria.where;
import static com.jayway.jsonpath.Filter.filter;

String json = "{\"store\":{\"book\":[{\"category\":\"fiction\",\"price\":10.99},{\"category\":\"reference\",\"price\":8.95}]}}";

// Pre-compiled JsonPath for reuse
JsonPath expensiveBooks = JsonPath.compile("$.store.book[?(@.price > 10)]");
assertThat(json, isJson(withJsonPath(expensiveBooks, hasSize(1))));

// Complex filtering with predicates
Predicate cheapFictionFilter = filter(
    where("category").is("fiction").and("price").lte(10D));
JsonPath cheapFiction = JsonPath.compile("$.store.book[?]", cheapFictionFilter);
assertThat(json, isJson(withJsonPath(cheapFiction, hasSize(0))));

// Combining multiple ReadContext matchers
assertThat(json, isJson(allOf(
    withJsonPath("$.store.book", hasSize(2)),
    withJsonPath("$.store.book[0].category", equalTo("fiction")),
    withoutJsonPath("$.store.magazine")
)));

Key Features

Indefinite Paths and Empty Results

When using indefinite path expressions (wildcards, array slices), results yield collections that may be empty:

String json = "{\"items\":[]}";

// Both assertions succeed - empty array is valid path result
assertThat(json, hasJsonPath("$.items[*]"));
assertThat(json, hasJsonPath("$.items[*].name"));

// Explicitly check for non-empty results when needed
assertThat(json, hasJsonPath("$.items[*]", hasSize(greaterThan(0))));

Null Value Handling

Null is a valid JSON value and paths containing null are considered valid:

String json = "{\"value\":null,\"missing\":undefined}";

// All succeed - null is a valid path value
assertThat(json, hasJsonPath("$.value"));
assertThat(json, hasJsonPath("$.value", nullValue()));
assertThat(json, isJson(withJsonPath("$.value", nullValue())));

// These fail - path doesn't exist
assertThat(json, not(hasJsonPath("$.missing")));
assertThat(json, not(isJson(withJsonPath("$.missing"))));

Configuration Integration

JsonPath evaluation depends on the current JsonPath configuration:

import com.jayway.jsonpath.Configuration;

// Configure JsonPath behavior globally
Configuration.setDefaults(customConfiguration);

// Matchers will use the configured settings for parsing and evaluation
assertThat(json, hasJsonPath("$.path", equalTo(expectedValue)));

Install with Tessl CLI

npx tessl i tessl/maven-com-jayway-jsonpath--json-path-assert

docs

collection-matchers.md

fluent-assertions.md

index.md

jsonpath-matchers.md

tile.json