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"));
}
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");
}
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);
}
Aggregations