I'd like to write a unit test for a web service using json-path-assert. Given this JSON:
[
["Some short name","Some parent","Some name"],
["Some short name 2","Some parent 2","Some name 2"]
]
I'd like to check that the parent of "Some name 2" is "Some parent 2". I cannot change the structure of JSON since it is dictated by a third-party library.
Here's the JSONPath expression I came up with:
$..[?(@[2] == 'Some name 2')][1]
This expression works fine and gives returns the expected result ('Some parent 2') in this JSON tool. However when using it in Java using the JSONPath library I get an empty result instead of the correct value:
import com.jayway.jsonpath.JsonPath;
import org.junit.Test;
public class TestJSONPath
{
@Test
public void testJsonPath() {
String json = "[[\"Some short name\",\"Some parent\",\"Some name\"]," +
"[\"Some short name 2\",\"Some parent 2\",\"Some name 2\"]]";
System.out.println(JsonPath.read(json, "$[1][1]")); // OK, returns 'Some parent 2'
System.out.println(JsonPath.read(json, "$..[?(@[2] == 'Some name 2')][1]")); // Not OK, returns an empty list
}
}
Here's my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Could you please help me with ideas how to overcome this issue?