Skip to content

Commit

Permalink
Support parsing JSON from non root field
Browse files Browse the repository at this point in the history
  • Loading branch information
felixbarny committed Aug 18, 2023
1 parent 6729c5b commit c0769d1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static void apply(
ConflictStrategy conflictStrategy,
boolean strictJsonParsing
) {
Object value = apply(ctx.get(fieldName), allowDuplicateKeys, strictJsonParsing);
Object value = apply(new WriteField(fieldName, () -> ctx).get(null), allowDuplicateKeys, strictJsonParsing);
if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,20 @@ public void testApplyWithInvalidJson() {
assertThat(result, equalTo(1268));
}
}

public void testJsonInNonRootField() throws Exception {
String processorTag = randomAlphaOfLength(3);
JsonProcessor jsonProcessor = new JsonProcessor(processorTag, null, "event.original", null, true, REPLACE, false);

Map<String, Object> document = new HashMap<>();
Map<String, Object> event = new HashMap<>();
event.put("original", "{\"foo\": \"bar\"}");
document.put("event", event);

IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
jsonProcessor.execute(ingestDocument);

assertEquals("bar", ingestDocument.getFieldValue("foo", String.class));
assertEquals("{\"foo\": \"bar\"}", ingestDocument.getFieldValue("event.original", String.class));
}
}

0 comments on commit c0769d1

Please sign in to comment.