Search in sources :

Example 56 with JsonPath

use of com.jayway.jsonpath.JsonPath in project graylog2-server by Graylog2.

the class HTTPJSONPathDataAdapterTest method parseBodyWithMapMultiValue.

@Test
public void parseBodyWithMapMultiValue() throws Exception {
    final JsonPath singlePath = JsonPath.compile("$.hello");
    final JsonPath multiPath = JsonPath.compile("$.map");
    final LookupResult result = HTTPJSONPathDataAdapter.parseBody(singlePath, multiPath, body);
    assertThat(result.isEmpty()).isFalse();
    assertThat(result.hasError()).isFalse();
    assertThat(result.singleValue()).isEqualTo("world");
    assertThat(result.multiValue()).isNotNull();
    assertThat(result.multiValue()).isInstanceOf(Map.class);
    assertThat(result.multiValue()).containsOnly(entry("key1", "value1"), entry("key2", "value2"));
}
Also used : LookupResult(org.graylog2.plugin.lookup.LookupResult) JsonPath(com.jayway.jsonpath.JsonPath) Test(org.junit.Test)

Example 57 with JsonPath

use of com.jayway.jsonpath.JsonPath in project graylog2-server by Graylog2.

the class HTTPJSONPathDataAdapterTest method parseBodyWithListMultiValue.

@Test
public void parseBodyWithListMultiValue() throws Exception {
    final JsonPath singlePath = JsonPath.compile("$.hello");
    final JsonPath multiPath = JsonPath.compile("$.list");
    final LookupResult result = HTTPJSONPathDataAdapter.parseBody(singlePath, multiPath, body);
    assertThat(result.isEmpty()).isFalse();
    assertThat(result.hasError()).isFalse();
    assertThat(result.singleValue()).isEqualTo("world");
    assertThat(result.multiValue()).isNotNull();
    assertThat(result.multiValue()).isInstanceOf(Map.class);
    assertThat(result.multiValue()).containsKey("value");
    // noinspection ConstantConditions
    assertThat(result.multiValue().get("value")).isInstanceOf(Collection.class);
    // noinspection unchecked,ConstantConditions
    assertThat((Collection) result.multiValue().get("value")).containsOnly("a", "b", "c");
    assertThat(result.stringListValue()).containsOnly("a", "b", "c");
}
Also used : LookupResult(org.graylog2.plugin.lookup.LookupResult) Collection(java.util.Collection) JsonPath(com.jayway.jsonpath.JsonPath) Test(org.junit.Test)

Example 58 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)

Aggregations

JsonPath (com.jayway.jsonpath.JsonPath)58 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