Source:
public GenericRecord getOutputRecord(final JsonNode jsonNode, Schema schema) { GenericRecordBuilder outputRecord = new GenericRecordBuilder(schema); final Iterator<Map.Entry<String, JsonNode>> elements = jsonNode.fields(); Map.Entry<String, JsonNode> mapEntry; while (elements.hasNext()) { mapEntry = elements.next(); final JsonNode nextNode = mapEntry.getValue(); if (!(nextNode instanceof NullNode)) { if (nextNode instanceof TextNode) { outputRecord.set(mapEntry.getKey(), nextNode.textValue()); } else if (nextNode instanceof ObjectNode) { Schema schemaTo = jsonSchemaInferrer.inferSchema(nextNode.toString()); GenericRecord record = getOutputRecord(nextNode, schemaTo); outputRecord.set(mapEntry.getKey(), record); } else if (nextNode instanceof ArrayNode) { List<Object> listRecords = new ArrayList<Object>(); Iterator<JsonNode> elementsIterator = ((ArrayNode) nextNode).elements(); while (elementsIterator.hasNext()) { JsonNode nodeTo = elementsIterator.next(); if (nodeTo instanceof TextNode) { listRecords.add(nodeTo.textValue()); } else { Schema schemaTo = jsonSchemaInferrer.inferSchema(nodeTo.toString()); listRecords.add(getOutputRecord(nodeTo, schemaTo)); } } outputRecord.set(mapEntry.getKey(), listRecords); } } else { outputRecord.set(mapEntry.getKey(), null); } } return outputRecord.build(); }

Target:
private GenericRecord getOutputRecord(final JsonNode jsonNode, Schema schema) { GenericRecordBuilder outputRecord = new GenericRecordBuilder(schema); final Iterator<Map.Entry<String, JsonNode>> elements = jsonNode.fields(); Map.Entry<String, JsonNode> mapEntry; while (elements.hasNext()) { mapEntry = elements.next(); final JsonNode nextNode = mapEntry.getValue(); if (!(nextNode instanceof NullNode)) { if (nextNode instanceof ValueNode) { if (nextNode instanceof TextNode) { outputRecord.set(mapEntry.getKey(), nextNode.textValue()); } else if (nextNode instanceof IntNode) { outputRecord.set(mapEntry.getKey(), nextNode.intValue()); } else if (nextNode instanceof LongNode) { outputRecord.set(mapEntry.getKey(), nextNode.longValue()); } else if (nextNode instanceof DoubleNode) { outputRecord.set(mapEntry.getKey(), nextNode.doubleValue()); } } else if (nextNode instanceof ObjectNode) { Schema schemaTo = jsonSchemaInferrer.inferSchema(nextNode.toString()); GenericRecord record = getOutputRecord(nextNode, schemaTo); outputRecord.set(mapEntry.getKey(), record); } else if (nextNode instanceof ArrayNode) { List<Object> listRecords = new ArrayList<Object>(); Iterator<JsonNode> elementsIterator = ((ArrayNode) nextNode).elements(); while (elementsIterator.hasNext()) { JsonNode nodeTo = elementsIterator.next(); if (nodeTo instanceof ValueNode) { if (nodeTo instanceof TextNode) { listRecords.add(nodeTo.textValue()); } else if (nodeTo instanceof IntNode) { listRecords.add(nodeTo.intValue()); } else if (nodeTo instanceof LongNode) { listRecords.add(nodeTo.longValue()); } else if (nodeTo instanceof DoubleNode) { listRecords.add(nodeTo.doubleValue()); } } else { Schema schemaTo = jsonSchemaInferrer.inferSchema(nodeTo.toString()); listRecords.add(getOutputRecord(nodeTo, schemaTo)); } } outputRecord.set(mapEntry.getKey(), listRecords); } } else { outputRecord.set(mapEntry.getKey(), null); } } return outputRecord.build(); }