Search in sources :

Example 1 with FastJsonReader

use of org.pentaho.di.trans.steps.jsoninput.reader.FastJsonReader in project pentaho-kettle by pentaho.

the class JsonInput method createReader.

private void createReader() throws KettleException {
    data.reader = new FastJsonReader(meta.getInputFields(), log);
    data.reader.setIgnoreMissingPath(meta.isIgnoreMissingPath());
}
Also used : FastJsonReader(org.pentaho.di.trans.steps.jsoninput.reader.FastJsonReader)

Example 2 with FastJsonReader

use of org.pentaho.di.trans.steps.jsoninput.reader.FastJsonReader in project pentaho-kettle by pentaho.

the class JsonInputTest method testJsonInputPathResolution.

@Test
public void testJsonInputPathResolution() throws KettleException {
    JsonInputField inputField = new JsonInputField("value");
    final String PATH = "$[*].name";
    inputField.setPath(PATH);
    inputField.setType(ValueMetaInterface.TYPE_STRING);
    JsonInputMeta inputMeta = createSimpleMeta("json", inputField);
    VariableSpace variables = new Variables();
    JsonInput jsonInput = null;
    try {
        jsonInput = createJsonInput("json", inputMeta, variables, new Object[] { getSampleJson() });
        JsonInputData data = (JsonInputData) Whitebox.getInternalState(jsonInput, "data");
        FastJsonReader reader = (FastJsonReader) Whitebox.getInternalState(data, "reader");
        RowSet rowset = reader.parse(new ByteArrayInputStream(getSampleJson().getBytes()));
        List results = (List) Whitebox.getInternalState(rowset, "results");
        JSONArray jsonResult = (JSONArray) results.get(0);
        assertEquals(1, jsonResult.size());
        assertEquals("United States of America", jsonResult.get(0));
    } catch (InvalidPathException pathException) {
        assertNull(jsonInput);
    }
}
Also used : VariableSpace(org.pentaho.di.core.variables.VariableSpace) RowSet(org.pentaho.di.core.RowSet) JSONArray(net.minidev.json.JSONArray) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) FastJsonReader(org.pentaho.di.trans.steps.jsoninput.reader.FastJsonReader) InvalidPathException(com.jayway.jsonpath.InvalidPathException) Variables(org.pentaho.di.core.variables.Variables) ByteArrayInputStream(java.io.ByteArrayInputStream) FileObject(org.apache.commons.vfs2.FileObject) List(java.util.List) ArrayList(java.util.ArrayList) FileInputList(org.pentaho.di.core.fileinput.FileInputList) Test(org.junit.Test)

Example 3 with FastJsonReader

use of org.pentaho.di.trans.steps.jsoninput.reader.FastJsonReader in project pentaho-kettle by pentaho.

the class JsonInput method init.

@Override
protected boolean init() {
    data.rownr = 1L;
    data.nrInputFields = meta.getInputFields().length;
    data.repeatedFields = new BitSet(data.nrInputFields);
    for (int i = 0; i < data.nrInputFields; i++) {
        JsonInputField field = meta.getInputFields()[i];
        if (field.isRepeated()) {
            data.repeatedFields.set(i);
        }
    }
    try {
        // Init a new JSON reader
        data.reader = new FastJsonReader(this, meta.getInputFields(), meta.isDefaultPathLeafToNull(), meta.isIgnoreMissingPath(), meta.isIncludeNulls(), log);
    } catch (KettleException e) {
        logError(e.getMessage());
        return false;
    }
    return true;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) BitSet(java.util.BitSet) FastJsonReader(org.pentaho.di.trans.steps.jsoninput.reader.FastJsonReader)

Aggregations

FastJsonReader (org.pentaho.di.trans.steps.jsoninput.reader.FastJsonReader)3 InvalidPathException (com.jayway.jsonpath.InvalidPathException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 List (java.util.List)1 JSONArray (net.minidev.json.JSONArray)1 FileObject (org.apache.commons.vfs2.FileObject)1 Test (org.junit.Test)1 RowSet (org.pentaho.di.core.RowSet)1 KettleException (org.pentaho.di.core.exception.KettleException)1 FileInputList (org.pentaho.di.core.fileinput.FileInputList)1 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)1 VariableSpace (org.pentaho.di.core.variables.VariableSpace)1 Variables (org.pentaho.di.core.variables.Variables)1