org.valid4j:json-path-matchers

Matchers to test your JSON using json path expressions (based on jayway.jsonpath)

License

License

Categories

Categories

JSON Data
GroupId

GroupId

org.valid4j
ArtifactId

ArtifactId

json-path-matchers
Last Version

Last Version

1.1
Release Date

Release Date

Type

Type

jar
Description

Description

org.valid4j:json-path-matchers
Matchers to test your JSON using json path expressions (based on jayway.jsonpath)
Project URL

Project URL

https://github.com/valid4j/json-path-matchers
Project Organization

Project Organization

valid4j
Source Code Management

Source Code Management

http://github.com/valid4j/json-path-matchers/tree/master

Download json-path-matchers

How to add to project

<!-- https://jarcasting.com/artifacts/org.valid4j/json-path-matchers/ -->
<dependency>
    <groupId>org.valid4j</groupId>
    <artifactId>json-path-matchers</artifactId>
    <version>1.1</version>
</dependency>
// https://jarcasting.com/artifacts/org.valid4j/json-path-matchers/
implementation 'org.valid4j:json-path-matchers:1.1'
// https://jarcasting.com/artifacts/org.valid4j/json-path-matchers/
implementation ("org.valid4j:json-path-matchers:1.1")
'org.valid4j:json-path-matchers:jar:1.1'
<dependency org="org.valid4j" name="json-path-matchers" rev="1.1">
  <artifact name="json-path-matchers" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.valid4j', module='json-path-matchers', version='1.1')
)
libraryDependencies += "org.valid4j" % "json-path-matchers" % "1.1"
[org.valid4j/json-path-matchers "1.1"]

Dependencies

compile (2)

Group / Artifact Type Version
org.hamcrest : hamcrest-library jar 1.3
com.jayway.jsonpath : json-path jar 2.1.0

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
commons-io : commons-io jar 2.4

Project Modules

There are no modules declared in this project.

json-path-matchers

A library with hamcrest-matchers for testing JSON using json path expressions, based on JsonPath.

Getting started

This library is available at Maven Central Repository. Add this dependency to your pom.xml

<dependency>
    <groupId>org.valid4j</groupId>
    <artifactId>json-path-matchers</artifactId>
    <version>1.1</version>
</dependency>

Correlation between different artifacts (i.e. compile dependency):

org.valid4j:json-path-matchers com.jayway.jsonpath:json-path
1.1 2.1.0
1.0 2.0.0

Usage guide

Statically import the library entry point:

import static org.valid4j.matchers.jsonpath.JsonPathMatchers.*;

NOTE: The actual evaluation of JsonPath will depend on the current configuration:

Configuration.setDefaults(...);

The matchers can be used to inspect different representations of JSON:

// As a String...
String json = ...;

// or a file...
File json = ...;

// or an already parsed json object...
Object json = Configuration.defaultConfiguration().jsonProvider().parse(content);

Usage examples:

// Verify validity of JSON
assertThat(json, isJson());

// Verify existence (or non-existence) of JSON path
assertThat(json, hasJsonPath("$.message"));
assertThat(json, hasNoJsonPath("$.message"));

// Verify evaluation of JSON path
assertThat(json, hasJsonPath("$.message", equalTo("Hi there")));
assertThat(json, hasJsonPath("$.quantity", equalTo(5)));
assertThat(json, hasJsonPath("$.price", equalTo(34.56)));
assertThat(json, hasJsonPath("$.store.book[*].author", hasSize(4)));
assertThat(json, hasJsonPath("$.store.book[*].author", hasItem("Evelyn Waugh")));

Combine matchers for greater expressiveness

// This will separate the JSON parsing from the path evaluation
assertThat(json, isJson(withoutJsonPath("...")));
assertThat(json, isJson(withJsonPath("...", equalTo(3))));    

// Combine several JSON path evaluations into a single statement
// (This will parse the JSON only once)
assertThat(json, isJson(allOf(
    withJsonPath("$.store.name", equalTo("Little Shop")),
    withoutJsonPath("$.expensive"),
    withJsonPath("$..title", hasSize(4)))));

Match on pre-compiled complex JSON path expressions

Filter cheapFictionFilter = filter(
    where("category").is("fiction").and("price").lte(10D));
JsonPath cheapFiction = JsonPath.compile("$.store.book[?]", cheapFictionFilter);
String json = ...;
assertThat(json, isJson(withJsonPath(cheapFiction)));

Use typed matchers for specific JSON representations, if needed

String json = ...
assertThat(json, isJsonString(withJsonPath("$..author")));

File json = ...
assertThat(json, isJsonFile(withJsonPath("$..author")));

Project license

This software is licensed under Apache Software License, Version 2.0

org.valid4j

Versions

Version
1.1
1.0