-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse JSON Processor: README, more testing, support for JSON Pointer (#…
…1696) Signed-off-by: Finn Roblin <[email protected]>
- Loading branch information
1 parent
6d35560
commit 27e39d2
Showing
6 changed files
with
339 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Parse JSON Processor | ||
This is a processor that takes in an Event and parses its JSON data, including any nested fields. | ||
## Basic Usage | ||
To get started, create the following `pipelines.yaml`. | ||
```yaml | ||
parse-json-pipeline: | ||
source: | ||
stdin: | ||
processor: | ||
- json: | ||
sink: | ||
- stdout: | ||
``` | ||
#### Basic Example: | ||
If you wish to test the JSON Processor with the above config then you may find the following example useful. | ||
Run the pipeline and paste the following line into your console, and then enter `exit` on a new line. | ||
``` | ||
{"outer_key": {"inner_key": "inner_value"}} | ||
``` | ||
The processor will parse the message into the following: | ||
``` | ||
{"message": {"outer_key": {"inner_key": "inner_value"}}", "outer_key":{"inner_key":"inner_value"}}} | ||
``` | ||
#### Example with JSON Pointer: | ||
If you wish to parse a selection of the JSON data, you can specify a JSON Pointer using the `pointer` option in the configuration. | ||
The following configuration file and example demonstrates a basic pointer use case. | ||
```yaml | ||
parse-json-pipeline: | ||
source: | ||
stdin: | ||
processor: | ||
- json: | ||
pointer: "outer_key/inner_key" | ||
sink: | ||
- stdout: | ||
``` | ||
Run the pipeline and paste the following line into your console, and then enter `exit` on a new line. | ||
``` | ||
{"outer_key": {"inner_key": "inner_value"}} | ||
``` | ||
|
||
The processor will parse the message into the following: | ||
``` | ||
{"message": {"outer_key": {"inner_key": "inner_value"}}", "inner_key": "inner_value"} | ||
``` | ||
## Configuration | ||
* `source` (Optional) — The field in the `Event` that will be parsed. | ||
* Default: `message` | ||
|
||
* `destination` (Optional) — The destination field of the parsed JSON. Defaults to the root of the `Event`. | ||
* Defaults to writing to the root of the `Event` (The processor will write to root when `destination` is `null`). | ||
* Cannot be `""`, `/`, or any whitespace-only `String` because these are not valid `Event` fields. | ||
|
||
* `pointer` (Optional) — A JSON Pointer to the field to be parsed. | ||
* There is no `pointer` by default, meaning the entire `source` is parsed. | ||
* The `pointer` can access JSON Array indices as well. | ||
* If the JSON Pointer is invalid then the entire `source` data is parsed into the outgoing `Event`. | ||
* If the pointed-to key already exists in the `Event` and the `destination` is the root, then the entire path of the key will be used. | ||
|
||
## Developer Guide | ||
This plugin is compatible with Java 8 and up. See | ||
- [CONTRIBUTING](https://github.com/opensearch-project/data-prepper/blob/main/CONTRIBUTING.md) | ||
- [monitoring](https://github.com/opensearch-project/data-prepper/blob/main/docs/monitoring.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...java/com/amazon/dataprepper/plugins/processor/parsejson/ParseJsonProcessorConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugins.processor.parsejson; | ||
|
||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.amazon.dataprepper.plugins.processor.parsejson.ParseJsonProcessorConfig.DEFAULT_SOURCE; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import static com.amazon.dataprepper.test.helper.ReflectivelySetField.setField; | ||
|
||
public class ParseJsonProcessorConfigTest { | ||
|
||
private ParseJsonProcessorConfig createObjectUnderTest() { | ||
return new ParseJsonProcessorConfig(); | ||
} | ||
|
||
@Test | ||
public void test_when_defaultParseJsonProcessorConfig_then_returns_default_values() { | ||
final ParseJsonProcessorConfig objectUnderTest = createObjectUnderTest(); | ||
|
||
assertThat(objectUnderTest.getSource(), equalTo(DEFAULT_SOURCE)); | ||
assertThat(objectUnderTest.getDestination(), equalTo(null)); | ||
assertThat(objectUnderTest.getPointer(), equalTo(null)); | ||
} | ||
|
||
@Nested | ||
class Validation { | ||
final ParseJsonProcessorConfig config = createObjectUnderTest(); | ||
|
||
@Test | ||
void test_when_destinationIsWhiteSpaceOrFrontSlash_then_isValidDestinationFalse() | ||
throws NoSuchFieldException, IllegalAccessException { | ||
setField(ParseJsonProcessorConfig.class, config, "destination", "good destination"); | ||
|
||
assertThat(config.isValidDestination(), equalTo(true)); | ||
|
||
setField(ParseJsonProcessorConfig.class, config, "destination", ""); | ||
|
||
assertThat(config.isValidDestination(), equalTo(false)); | ||
|
||
setField(ParseJsonProcessorConfig.class, config, "destination", " "); | ||
|
||
assertThat(config.isValidDestination(), equalTo(false)); | ||
|
||
setField(ParseJsonProcessorConfig.class, config, "destination", " / "); | ||
|
||
assertThat(config.isValidDestination(), equalTo(false)); | ||
} | ||
} | ||
} |
Oops, something went wrong.