Search in sources :

Example 31 with JsonPath

use of com.jayway.jsonpath.JsonPath in project nifi by apache.

the class JsonPathEvaluator method evaluate.

@Override
public QueryResult<String> evaluate(final Map<String, String> attributes) {
    final String subjectValue = subject.evaluate(attributes).getValue();
    if (subjectValue == null || subjectValue.length() == 0) {
        throw new AttributeExpressionLanguageException("Subject is empty");
    }
    DocumentContext documentContext = null;
    try {
        documentContext = validateAndEstablishJsonContext(subjectValue);
    } catch (InvalidJsonException e) {
        throw new AttributeExpressionLanguageException("Subject contains invalid JSON: " + subjectValue, e);
    }
    final JsonPath compiledJsonPath;
    if (precompiledJsonPathExp != null) {
        compiledJsonPath = precompiledJsonPathExp;
    } else {
        compiledJsonPath = compileJsonPathExpression(jsonPathExp.evaluate(attributes).getValue());
    }
    Object result = null;
    try {
        result = documentContext.read(compiledJsonPath);
    } catch (Exception e) {
        // assume the path did not match anything in the document
        return EMPTY_RESULT;
    }
    return new StringQueryResult(getResultRepresentation(result, EMPTY_RESULT.getValue()));
}
Also used : StringQueryResult(org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult) AttributeExpressionLanguageException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException) InvalidJsonException(com.jayway.jsonpath.InvalidJsonException) DocumentContext(com.jayway.jsonpath.DocumentContext) JsonPath(com.jayway.jsonpath.JsonPath) AttributeExpressionLanguageException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException) InvalidJsonException(com.jayway.jsonpath.InvalidJsonException)

Example 32 with JsonPath

use of com.jayway.jsonpath.JsonPath in project nifi by apache.

the class JsonPathReader method compileJsonPaths.

@OnEnabled
public void compileJsonPaths(final ConfigurationContext context) {
    this.dateFormat = context.getProperty(DateTimeUtils.DATE_FORMAT).getValue();
    this.timeFormat = context.getProperty(DateTimeUtils.TIME_FORMAT).getValue();
    this.timestampFormat = context.getProperty(DateTimeUtils.TIMESTAMP_FORMAT).getValue();
    final LinkedHashMap<String, JsonPath> compiled = new LinkedHashMap<>();
    for (final PropertyDescriptor descriptor : context.getProperties().keySet()) {
        if (!descriptor.isDynamic()) {
            continue;
        }
        final String fieldName = descriptor.getName();
        final String expression = context.getProperty(descriptor).getValue();
        final JsonPath jsonPath = JsonPath.compile(expression);
        compiled.put(fieldName, jsonPath);
    }
    jsonPaths = compiled;
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) JsonPath(com.jayway.jsonpath.JsonPath) LinkedHashMap(java.util.LinkedHashMap) OnEnabled(org.apache.nifi.annotation.lifecycle.OnEnabled)

Example 33 with JsonPath

use of com.jayway.jsonpath.JsonPath in project nifi by apache.

the class JsonPathRowRecordReader method convertJsonNodeToRecord.

@Override
protected Record convertJsonNodeToRecord(final JsonNode jsonNode, final RecordSchema schema, final boolean coerceTypes, final boolean dropUnknownFields) throws IOException {
    if (jsonNode == null) {
        return null;
    }
    final DocumentContext ctx = JsonPath.using(STRICT_PROVIDER_CONFIGURATION).parse(jsonNode.toString());
    final Map<String, Object> values = new HashMap<>(schema.getFieldCount());
    for (final Map.Entry<String, JsonPath> entry : jsonPaths.entrySet()) {
        final String fieldName = entry.getKey();
        final DataType desiredType = schema.getDataType(fieldName).orElse(null);
        if (desiredType == null && dropUnknownFields) {
            continue;
        }
        final JsonPath jsonPath = entry.getValue();
        Object value;
        try {
            value = ctx.read(jsonPath);
        } catch (final PathNotFoundException pnfe) {
            logger.debug("Evaluated JSONPath Expression {} but the path was not found; will use a null value", new Object[] { entry.getValue() });
            value = null;
        }
        final Optional<RecordField> field = schema.getField(fieldName);
        final Object defaultValue = field.isPresent() ? field.get().getDefaultValue() : null;
        if (coerceTypes && desiredType != null) {
            value = convert(value, desiredType, fieldName, defaultValue);
        } else {
            final DataType dataType = field.isPresent() ? field.get().getDataType() : null;
            value = convert(value, dataType);
        }
        values.put(fieldName, value);
    }
    return new MapRecord(schema, values);
}
Also used : MapRecord(org.apache.nifi.serialization.record.MapRecord) RecordField(org.apache.nifi.serialization.record.RecordField) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JsonPath(com.jayway.jsonpath.JsonPath) DataType(org.apache.nifi.serialization.record.DataType) RecordDataType(org.apache.nifi.serialization.record.type.RecordDataType) ArrayDataType(org.apache.nifi.serialization.record.type.ArrayDataType) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) DocumentContext(com.jayway.jsonpath.DocumentContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 34 with JsonPath

use of com.jayway.jsonpath.JsonPath in project nifi by apache.

the class TestJsonPathRowRecordReader method testElementWithNestedData.

@Test
public void testElementWithNestedData() throws IOException, MalformedRecordException {
    final LinkedHashMap<String, JsonPath> jsonPaths = new LinkedHashMap<>(allJsonPaths);
    jsonPaths.put("account", JsonPath.compile("$.account"));
    final DataType accountType = RecordFieldType.RECORD.getRecordDataType(getAccountSchema());
    final List<RecordField> fields = getDefaultFields();
    fields.add(new RecordField("account", accountType));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    try (final InputStream in = new FileInputStream(new File("src/test/resources/json/single-element-nested.json"));
        final JsonPathRowRecordReader reader = new JsonPathRowRecordReader(jsonPaths, schema, in, Mockito.mock(ComponentLog.class), dateFormat, timeFormat, timestampFormat)) {
        final List<String> fieldNames = schema.getFieldNames();
        final List<String> expectedFieldNames = Arrays.asList(new String[] { "id", "name", "balance", "address", "city", "state", "zipCode", "country", "account" });
        assertEquals(expectedFieldNames, fieldNames);
        final List<RecordFieldType> dataTypes = schema.getDataTypes().stream().map(dt -> dt.getFieldType()).collect(Collectors.toList());
        final List<RecordFieldType> expectedTypes = Arrays.asList(new RecordFieldType[] { RecordFieldType.INT, RecordFieldType.STRING, RecordFieldType.DOUBLE, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.RECORD });
        assertEquals(expectedTypes, dataTypes);
        final Object[] firstRecordValues = reader.nextRecord().getValues();
        final Object[] simpleElements = Arrays.copyOfRange(firstRecordValues, 0, firstRecordValues.length - 1);
        Assert.assertArrayEquals(new Object[] { 1, "John Doe", null, "123 My Street", "My City", "MS", "11111", "USA" }, simpleElements);
        final Object lastElement = firstRecordValues[firstRecordValues.length - 1];
        assertTrue(lastElement instanceof Record);
        final Record record = (Record) lastElement;
        assertEquals(42, record.getValue("id"));
        assertEquals(4750.89D, record.getValue("balance"));
        assertNull(reader.nextRecord());
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Arrays(java.util.Arrays) DataType(org.apache.nifi.serialization.record.DataType) ComponentLog(org.apache.nifi.logging.ComponentLog) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Record(org.apache.nifi.serialization.record.Record) Before(org.junit.Before) MalformedRecordException(org.apache.nifi.serialization.MalformedRecordException) Assert.assertNotNull(org.junit.Assert.assertNotNull) RecordField(org.apache.nifi.serialization.record.RecordField) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test) FileInputStream(java.io.FileInputStream) JsonPath(com.jayway.jsonpath.JsonPath) Collectors(java.util.stream.Collectors) File(java.io.File) Mockito(org.mockito.Mockito) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert(org.junit.Assert) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) RecordField(org.apache.nifi.serialization.record.RecordField) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonPath(com.jayway.jsonpath.JsonPath) ComponentLog(org.apache.nifi.logging.ComponentLog) FileInputStream(java.io.FileInputStream) LinkedHashMap(java.util.LinkedHashMap) DataType(org.apache.nifi.serialization.record.DataType) Record(org.apache.nifi.serialization.record.Record) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) File(java.io.File) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) Test(org.junit.Test)

Example 35 with JsonPath

use of com.jayway.jsonpath.JsonPath in project nifi by apache.

the class TestJsonPathRowRecordReader method testReadArrayDifferentSchemasWithOverride.

@Test
public void testReadArrayDifferentSchemasWithOverride() throws IOException, MalformedRecordException {
    final LinkedHashMap<String, JsonPath> jsonPaths = new LinkedHashMap<>(allJsonPaths);
    jsonPaths.put("address2", JsonPath.compile("$.address2"));
    final List<RecordField> fields = getDefaultFields();
    fields.add(new RecordField("address2", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    try (final InputStream in = new FileInputStream(new File("src/test/resources/json/bank-account-array-different-schemas.json"));
        final JsonPathRowRecordReader reader = new JsonPathRowRecordReader(jsonPaths, schema, in, Mockito.mock(ComponentLog.class), dateFormat, timeFormat, timestampFormat)) {
        final List<String> fieldNames = schema.getFieldNames();
        final List<String> expectedFieldNames = Arrays.asList(new String[] { "id", "name", "balance", "address", "city", "state", "zipCode", "country", "address2" });
        assertEquals(expectedFieldNames, fieldNames);
        final List<RecordFieldType> dataTypes = schema.getDataTypes().stream().map(dt -> dt.getFieldType()).collect(Collectors.toList());
        final List<RecordFieldType> expectedTypes = Arrays.asList(new RecordFieldType[] { RecordFieldType.INT, RecordFieldType.STRING, RecordFieldType.DOUBLE, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING, RecordFieldType.STRING });
        assertEquals(expectedTypes, dataTypes);
        final Object[] firstRecordValues = reader.nextRecord().getValues();
        Assert.assertArrayEquals(new Object[] { 1, "John Doe", 4750.89, "123 My Street", "My City", "MS", "11111", "USA", null }, firstRecordValues);
        final Object[] secondRecordValues = reader.nextRecord().getValues();
        Assert.assertArrayEquals(new Object[] { 2, "Jane Doe", 4820.09, "321 Your Street", "Your City", "NY", "33333", null, null }, secondRecordValues);
        final Object[] thirdRecordValues = reader.nextRecord().getValues();
        Assert.assertArrayEquals(new Object[] { 3, "Jake Doe", 4751.89, "124 My Street", "My City", "MS", "11111", "USA", "Apt. #12" }, thirdRecordValues);
        assertNull(reader.nextRecord());
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Arrays(java.util.Arrays) DataType(org.apache.nifi.serialization.record.DataType) ComponentLog(org.apache.nifi.logging.ComponentLog) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Record(org.apache.nifi.serialization.record.Record) Before(org.junit.Before) MalformedRecordException(org.apache.nifi.serialization.MalformedRecordException) Assert.assertNotNull(org.junit.Assert.assertNotNull) RecordField(org.apache.nifi.serialization.record.RecordField) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test) FileInputStream(java.io.FileInputStream) JsonPath(com.jayway.jsonpath.JsonPath) Collectors(java.util.stream.Collectors) File(java.io.File) Mockito(org.mockito.Mockito) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert(org.junit.Assert) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) RecordField(org.apache.nifi.serialization.record.RecordField) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonPath(com.jayway.jsonpath.JsonPath) ComponentLog(org.apache.nifi.logging.ComponentLog) FileInputStream(java.io.FileInputStream) LinkedHashMap(java.util.LinkedHashMap) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) File(java.io.File) RecordFieldType(org.apache.nifi.serialization.record.RecordFieldType) 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