Browse Source

fix result if object does not contain key (fixes #270)

pull/331/head
Jochen Berger 8 years ago
parent
commit
b536af9b9a
  1. 3
      json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonOrgJsonProvider.java
  2. 8
      json-path/src/test/java/com/jayway/jsonpath/JsonOrgJsonProviderTest.java

3
json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonOrgJsonProvider.java

@ -96,11 +96,10 @@ public class JsonOrgJsonProvider extends AbstractJsonProvider {
public Object getMapValue(Object obj, String key) {
try {
JSONObject jsonObject = toJsonObject(obj);
Object o = jsonObject.get(key);
if (!jsonObject.has(key)) {
return UNDEFINED;
} else {
return unwrap(o);
return unwrap(jsonObject.get(key));
}
} catch (JSONException e) {
throw new JsonPathException(e);

8
json-path/src/test/java/com/jayway/jsonpath/JsonOrgJsonProviderTest.java

@ -44,4 +44,12 @@ public class JsonOrgJsonProviderTest extends BaseTest {
assertThat(books.size()).isEqualTo(4);
}
@Test
public void read_books_with_isbn() {
JSONArray books = using(JSON_ORG_CONFIGURATION).parse(JSON_DOCUMENT).read("$..book[?(@.isbn)]");
assertThat(books.length()).isEqualTo(2);
}
}

Loading…
Cancel
Save