use of com.jayway.jsonpath.JsonPath in project JsonPath by jayway.
the class JsonPathTest method read_store_book_1.
@Test
public void read_store_book_1() throws Exception {
JsonPath path = JsonPath.compile("$.store.book[1]");
Map map = path.read(DOCUMENT);
assertEquals("Evelyn Waugh", map.get("author"));
}
use of com.jayway.jsonpath.JsonPath in project JsonPath by jayway.
the class ApiResource method validate.
@GET
@Path("/validate")
@Produces(MediaType.APPLICATION_JSON)
public Response validate(@QueryParam("path") String path) {
int result = -1;
try {
JsonPath compiled = JsonPath.compile(path);
result = compiled.isDefinite() ? 0 : 1;
} catch (Exception e) {
}
return Response.ok(Collections.singletonMap("result", result)).build();
}
use of com.jayway.jsonpath.JsonPath in project camel by apache.
the class JsonPathEngine method read.
public Object read(Exchange exchange) throws Exception {
if (path == null) {
Expression exp = exchange.getContext().resolveLanguage("simple").createExpression(expression);
String text = exp.evaluate(exchange, String.class);
JsonPath path = JsonPath.compile(text);
LOG.debug("Compiled dynamic JsonPath: {}", expression);
return doRead(path, exchange);
} else {
return doRead(path, exchange);
}
}
use of com.jayway.jsonpath.JsonPath in project intellij-community by JetBrains.
the class JsonPathResponseHandler method extractRawValue.
@Nullable
private Object extractRawValue(@NotNull Selector selector, @NotNull String source) throws Exception {
if (StringUtil.isEmpty(selector.getPath())) {
return null;
}
JsonPath jsonPath = lazyCompile(selector.getPath());
Object value;
try {
value = jsonPath.read(source);
} catch (InvalidPathException e) {
throw new Exception(String.format("JsonPath expression '%s' doesn't match", selector.getPath()), e);
}
if (value == null) {
return null;
}
return value;
}
use of com.jayway.jsonpath.JsonPath in project jmeter by apache.
the class JSONManager method extractWithJsonPath.
/**
*
* @param jsonString JSON String from which data is extracted
* @param jsonPath JSON-PATH expression
* @return List of JSON Strings of the extracted data
* @throws ParseException when parsing fails
*/
public List<Object> extractWithJsonPath(String jsonString, String jsonPath) throws ParseException {
JsonPath jsonPathParser = getJsonPath(jsonPath);
List<Object> extractedObjects;
try {
extractedObjects = jsonPathParser.read(jsonString, DEFAULT_CONFIGURATION);
} catch (PathNotFoundException e) {
if (log.isDebugEnabled()) {
log.debug("Could not find JSON Path {} in [{}]: {}", jsonPath, jsonString, e.getLocalizedMessage());
}
return Collections.emptyList();
}
List<Object> results = new ArrayList<>(extractedObjects.size());
for (Object obj : extractedObjects) {
results.add(stringifyJSONObject(obj));
}
return results;
}
Aggregations