Search in sources :

Example 36 with JsonPath

use of com.jayway.jsonpath.JsonPath in project pentaho-kettle by pentaho.

the class FastJsonReader method compilePaths.

private static JsonPath[] compilePaths(JsonInputField[] fields) {
    JsonPath[] paths = new JsonPath[fields.length];
    int i = 0;
    for (JsonInputField field : fields) {
        paths[i++] = JsonPath.compile(field.getPath());
    }
    return paths;
}
Also used : JsonPath(com.jayway.jsonpath.JsonPath) JsonInputField(org.pentaho.di.trans.steps.jsoninput.JsonInputField)

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

Example 39 with JsonPath

use of com.jayway.jsonpath.JsonPath in project batfish by batfish.

the class JsonPathTest method testPath4.

@Test
public void testPath4() {
    JsonPath jsonPath = JsonPath.compile("$.nodes[*][?([\"1.2.3.4\"] subsetof @.loggingServers)].loggingServers");
    assertThat(jsonPath, not(equalTo(nullValue())));
}
Also used : JsonPath(com.jayway.jsonpath.JsonPath) Test(org.junit.Test)

Example 40 with JsonPath

use of com.jayway.jsonpath.JsonPath in project batfish by batfish.

the class JsonPathTest method testOneNtpServerPresent.

@Test
public void testOneNtpServerPresent() {
    String path = "$.nodes[*][?(!([\"1.2.3.4\"] subsetof @.ntpServers))].ntpServers";
    JsonPath jsonPath = JsonPath.compile(path);
    ArrayNode prefixes = null;
    try {
        prefixes = jsonPath.read(_oneNtpServerNodesAnswerJsonObject, _prefixConfiguration);
    } catch (PathNotFoundException e) {
        prefixes = JsonNodeFactory.instance.arrayNode();
    }
    assertThat(prefixes, not(equalTo(nullValue())));
    assertThat(prefixes.size(), equalTo(0));
}
Also used : ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) JsonPath(com.jayway.jsonpath.JsonPath) Test(org.junit.Test)

Aggregations

JsonPath (com.jayway.jsonpath.JsonPath)61 Test (org.junit.Test)34 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)13 BaseTest (com.jayway.jsonpath.BaseTest)12 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)9 ArrayList (java.util.ArrayList)9 LinkedHashMap (java.util.LinkedHashMap)8 List (java.util.List)8 Map (java.util.Map)8 DocumentContext (com.jayway.jsonpath.DocumentContext)6 IOException (java.io.IOException)6 ComponentLog (org.apache.nifi.logging.ComponentLog)6 File (java.io.File)5 Arrays (java.util.Arrays)5 HashMap (java.util.HashMap)5 Collectors (java.util.stream.Collectors)5 DataType (org.apache.nifi.serialization.record.DataType)5 RecordField (org.apache.nifi.serialization.record.RecordField)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 Assert.assertTrue (org.junit.Assert.assertTrue)5