public static JSONPathFieldSpec createNestedField(String name, String expr) { return new JSONPathFieldSpec(JSONPathFieldType.PATH, name, expr); }
public static JSONPathFieldSpec createRootField(String name) { return new JSONPathFieldSpec(JSONPathFieldType.ROOT, name, null); }
public static JSONPathFieldSpec createJqField(String name, String expr) { return new JSONPathFieldSpec(JSONPathFieldType.JQ, name, expr); }
@JsonCreator public JSONFlatDataParser( @JacksonInject @Json ObjectMapper jsonMapper, @JsonProperty("keyFieldName") final String keyFieldName, @JsonProperty("valueFieldName") final String valueFieldName ) { Preconditions.checkArgument(!Strings.isNullOrEmpty(keyFieldName), "[keyFieldName] cannot be empty"); Preconditions.checkArgument(!Strings.isNullOrEmpty(valueFieldName), "[valueFieldName] cannot be empty"); this.keyFieldName = keyFieldName; this.valueFieldName = valueFieldName; // Copy jsonMapper; don't want to share canonicalization tables, etc., with the global ObjectMapper. this.parser = new DelegateParser( new JSONPathParser( new JSONPathSpec( false, ImmutableList.of( new JSONPathFieldSpec(JSONPathFieldType.ROOT, keyFieldName, keyFieldName), new JSONPathFieldSpec(JSONPathFieldType.ROOT, valueFieldName, valueFieldName) ) ), jsonMapper.copy() ), keyFieldName, valueFieldName ); }
@Test public void testParseRowWithConditional() { final JSONParseSpec parseSpec = new JSONParseSpec( new TimestampSpec("timestamp", "iso", null), new DimensionsSpec(DimensionsSpec.getDefaultSchemas(ImmutableList.of("foo")), null, null), new JSONPathSpec( true, ImmutableList.of( new JSONPathFieldSpec(JSONPathFieldType.PATH, "foo", "$.[?(@.maybe_object)].maybe_object.foo.test"), new JSONPathFieldSpec(JSONPathFieldType.PATH, "baz", "$.maybe_object_2.foo.test"), new JSONPathFieldSpec(JSONPathFieldType.PATH, "bar", "$.[?(@.something_else)].something_else.foo") ) ), null ); final Map<String, Object> expected = new HashMap<>(); expected.put("foo", new ArrayList()); expected.put("baz", null); expected.put("bar", Collections.singletonList("test")); final Parser<String, Object> parser = parseSpec.makeParser(); final Map<String, Object> parsedRow = parser.parseToMap("{\"something_else\": {\"foo\": \"test\"}}"); Assert.assertNotNull(parsedRow); Assert.assertEquals(expected, parsedRow); }
true, ImmutableList.of( new JSONPathFieldSpec(JSONPathFieldType.ROOT, "root_baz", "baz"), new JSONPathFieldSpec(JSONPathFieldType.ROOT, "root_baz2", "baz2"), new JSONPathFieldSpec(JSONPathFieldType.PATH, "path_omg", "$.o.mg"), new JSONPathFieldSpec(JSONPathFieldType.PATH, "path_omg2", "$.o.mg2"), new JSONPathFieldSpec(JSONPathFieldType.JQ, "jq_omg", ".o.mg"), new JSONPathFieldSpec(JSONPathFieldType.JQ, "jq_omg2", ".o.mg2")
@Test public void testRejectDuplicates2() { List<JSONPathFieldSpec> fields = new ArrayList<>(); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "met-array", "$.met.a")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.JQ, "met-array", ".met.a")); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Cannot have duplicate field definition: met-array"); final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(false, fields), null); jsonParser.parseToMap(nestedJson); }
@Test public void testRejectDuplicates() { List<JSONPathFieldSpec> fields = new ArrayList<>(); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "met-array", "$.met.a")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "met-array", "$.met.a")); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Cannot have duplicate field definition: met-array"); final Parser<String, Object> jsonParser = new JSONPathParser(new JSONPathSpec(false, fields), null); jsonParser.parseToMap(nestedJson); }
fields.add(new JSONPathFieldSpec(JSONPathFieldType.ROOT, "simpleVal", "simpleVal")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.ROOT, "timestamp", "timestamp")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "nested-foo.bar2", "$.foo.bar2")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "heybarx0", "$.hey[0].barx")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "met-array", "$.met.a")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.JQ, "jq-nested-foo.bar2", ".foo.bar2")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.JQ, "jq-heybarx0", ".hey[0].barx")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.JQ, "jq-met-array", ".met.a"));
fields.add(new JSONPathFieldSpec(JSONPathFieldType.ROOT, "baz", "baz")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "nested-foo.bar1", "$.foo.bar1")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "nested-foo.bar2", "$.foo.bar2")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "heybarx0", "$.hey[0].barx")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "met-array", "$.met.a")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.ROOT, "testListConvert2", "testListConvert2")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.ROOT, "testMapConvert", "testMapConvert")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.ROOT, "INVALID_ROOT", "INVALID_ROOT_EXPR")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.PATH, "INVALID_PATH", "INVALID_PATH_EXPR")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.JQ, "jq-nested-foo.bar1", ".foo.bar1")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.JQ, "jq-nested-foo.bar2", ".foo.bar2")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.JQ, "jq-heybarx0", ".hey[0].barx")); fields.add(new JSONPathFieldSpec(JSONPathFieldType.JQ, "jq-met-array", ".met.a"));
public static JSONPathFieldSpec createJqField(String name, String expr) { return new JSONPathFieldSpec(JSONPathFieldType.JQ, name, expr); }
public static JSONPathFieldSpec createNestedField(String name, String expr) { return new JSONPathFieldSpec(JSONPathFieldType.PATH, name, expr); }
public static JSONPathFieldSpec createRootField(String name) { return new JSONPathFieldSpec(JSONPathFieldType.ROOT, name, null); }