@Test public void testJSONFunctions() throws Exception { List<Row> rows = Arrays.asList( new Row("body", "{\n" + " \"name\" : {\n" + " \"Fname\" : \"Joltie\",\n" + " \"Lname\" : \"Root\",\n" + " \"mname\" : null\n" + " },\n" + " \"coordinates\" : [\n" + " 12.56,\n" + " 45.789\n" + " ],\n" + " \"numbers\" : [\n" + " 1,\n" + " 2.1,\n" + " 3,\n" + " null,\n" + " 4,\n" + " 5,\n" + " 6,\n" + " null\n" + " ],\n" + " \"responses\" : [\n" + " { \"a\" : 1, \"b\" : \"X\", \"c\" : 2.8},\n" + " { \"a\" : 2, \"b\" : \"Y\", \"c\" : 232342.8},\n" + " { \"a\" : 3, \"b\" : \"Z\", \"c\" : null},\n" + " { \"a\" : 4, \"b\" : \"U\"}\n" + " ],\n" + " \"integer\" : 1,\n" + " \"double\" : 2.8,\n" + " \"float\" : 45.6,\n" + " \"aliases\" : [\n" + " \"root\",\n" + " \"joltie\",\n" + " \"bunny\",\n" + " null\n" + " ]\n" + "}") ); String[] directives = new String[] { "set-column s0 json:select(body, true, \"$.name.fname\", \"$.name.lname\")", "set-column s1 json:select(body, true, \"$.name.fname\")", "set-column s11 json:select(body, true, \"$.numbers\")", "set-column s2 json:select(body, true, \"$.numbers\")", "set-column s4 json:drop(body, \"numbers\", \"integer\", \"float\", \"aliases\", \"name\")", "set-column s5 json:join(s11, \":\")" }; rows = TestingRig.execute(directives, rows); Assert.assertTrue(rows.size() == 1); }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { List<Row> results = new ArrayList<>(); for (Row row : rows) { Object value = row.getValue(src); if (value == null) { row.add(dest, null); continue; } if (!(value instanceof String || value instanceof JsonObject || value instanceof JsonArray)) { throw new DirectiveExecutionException( NAME, String.format("Column '%s' is of invalid type '%s'. It should be of type 'String' " + "or 'JsonObject' or 'JsonArray'.", src, value.getClass().getSimpleName()) ); } JsonElement element = parser.parse(value).read(path); Object val = JsParser.getValue(element); int pos = row.find(dest); if (pos == -1) { row.add(dest, val); } else { row.setValue(pos, val); } results.add(row); } return results; }
JsPath implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { List<Row> results = new ArrayList<>(); for (Row row : rows) { Object value = row.getValue(src); if (value == null) { row.add(dest, null); continue; } if (!(value instanceof String || value instanceof JsonObject || value instanceof JsonArray)) { throw new DirectiveExecutionException( NAME, String.format("Column '%s' is of invalid type '%s'. It should be of type 'String' " + "or 'JsonObject' or 'JsonArray'.", src, value.getClass().getSimpleName()) ); } JsonElement element = parser.parse(value).read(path); Object val = JsParser.getValue(element); int pos = row.find(dest); if (pos == -1) { row.add(dest, val); } else { row.setValue(pos, val); } results.add(row); } return results; } }
JsPath implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { List<Row> results = new ArrayList<>(); for (Row row : rows) { Object value = row.getValue(src); if (value == null) { row.add(dest, null); continue; } if (!(value instanceof String || value instanceof JsonObject || value instanceof JsonArray)) { throw new DirectiveExecutionException( NAME, String.format("Column '%s' is of invalid type '%s'. It should be of type 'String' " + "or 'JsonObject' or 'JsonArray'.", src, value.getClass().getSimpleName()) ); } JsonElement element = parser.parse(value).read(path); Object val = JsParser.getValue(element); int pos = row.find(dest); if (pos == -1) { row.add(dest, val); } else { row.setValue(pos, val); } results.add(row); } return results; }  }
JsPath implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { List<Row> results = new ArrayList<>(); for (Row row : rows) { Object value = row.getValue(src); if (value == null) { row.add(dest, null); continue; } if (!(value instanceof String || value instanceof JsonObject || value instanceof JsonArray)) { throw new DirectiveExecutionException( NAME, String.format("Column '%s' is of invalid type '%s'. It should be of type 'String' " + "or 'JsonObject' or 'JsonArray'.", src, value.getClass().getSimpleName()) ); } JsonElement element = parser.parse(value).read(path); Object val = JsParser.getValue(element); int pos = row.find(dest); if (pos == -1) { row.add(dest, val); } else { row.setValue(pos, val); } results.add(row); } return results; }  @Override UsageDefinition define(); @Override void initialize(Arguments args); @Override void destroy(); @Override List<Row> execute(List<Row> rows, ExecutorContext context); @Override Mutation lineage(); }
JsPath implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { List<Row> results = new ArrayList<>(); for (Row row : rows) { Object value = row.getValue(src); if (value == null) { row.add(dest, null); continue; } if (!(value instanceof String || value instanceof JsonObject || value instanceof JsonArray)) { throw new DirectiveExecutionException( NAME, String.format("Column '%s' is of invalid type '%s'. It should be of type 'String' " + "or 'JsonObject' or 'JsonArray'.", src, value.getClass().getSimpleName()) ); } JsonElement element = parser.parse(value).read(path); Object val = JsParser.getValue(element); int pos = row.find(dest); if (pos == -1) { row.add(dest, val); } else { row.setValue(pos, val); } results.add(row); } return results; }  @Override UsageDefinition define(); @Override void initialize(Arguments args); @Override void destroy(); @Override List<Row> execute(List<Row> rows, ExecutorContext context); @Override Mutation lineage(); static final String NAME; static final Configuration GSON_CONFIGURATION; }