Search in sources :

Example 11 with JsonPath

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"));
}
Also used : JsonPath(com.jayway.jsonpath.JsonPath) Map(java.util.Map) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 12 with JsonPath

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();
}
Also used : JsonPath(com.jayway.jsonpath.JsonPath) Path(javax.ws.rs.Path) JsonPath(com.jayway.jsonpath.JsonPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 13 with JsonPath

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);
    }
}
Also used : Expression(org.apache.camel.Expression) JsonPath(com.jayway.jsonpath.JsonPath)

Example 14 with JsonPath

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;
}
Also used : JsonPath(com.jayway.jsonpath.JsonPath) InvalidPathException(com.jayway.jsonpath.InvalidPathException) InvalidPathException(com.jayway.jsonpath.InvalidPathException) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with JsonPath

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;
}
Also used : ArrayList(java.util.ArrayList) JSONObject(net.minidev.json.JSONObject) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) JsonPath(com.jayway.jsonpath.JsonPath)

Aggregations

JsonPath (com.jayway.jsonpath.JsonPath)15 BaseTest (com.jayway.jsonpath.BaseTest)5 Test (org.junit.Test)5 InvalidPathException (com.jayway.jsonpath.InvalidPathException)2 LRUCache (com.jayway.jsonpath.spi.cache.LRUCache)2 Pair (io.druid.java.util.common.Pair)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 JsonObject (com.google.gson.JsonObject)1 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)1 Cache (com.jayway.jsonpath.spi.cache.Cache)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 JSONObject (net.minidev.json.JSONObject)1 Expression (org.apache.camel.Expression)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1