JSON processor

The json processor serializes a string value field into a map of maps, which can be useful for various data processing and enrichment tasks.

The following is the syntax for the json processor:

  1. {
  2. "processor": {
  3. "json": {
  4. "field": "<field_name>",
  5. "target_field": "<target_field_name>",
  6. "add_to_root": <boolean>
  7. }
  8. }
  9. }

copy

Configuration parameters

The following table lists the required and optional parameters for the json processor.

ParameterRequired/OptionalDescription
fieldRequiredThe name of the field containing the JSON-formatted string to be deserialized.
target_fieldOptionalThe name of the field in which the deserialized JSON data is stored. When not provided, the data is stored in the field field. If target_field exists, its existing value is overwritten with the new JSON data.
add_to_rootOptionalA Boolean flag that determines whether the deserialized JSON data should be added to the root of the document (true) or stored in the target_field (false). If add_to_root is true, then target-field is invalid. Default value is false.
descriptionOptionalA description of the processor’s purpose or configuration.
ifOptionalSpecifies to conditionally execute the processor.
ignore_failureOptionalSpecifies to ignore processor failures. See Handling pipeline failures.
on_failureOptionalSpecifies a list of processors to run if the processor fails during execution. These processors are executed in the order they are specified.
tagOptionalAn identifier tag for the processor. Useful for debugging in order to distinguish between processors of the same type.

Using the processor

Follow these steps to use the processor in a pipeline.

Step 1: Create a pipeline

The following query creates a pipeline named my-json-pipeline that uses the json processor to process JSON data and enrich the documents with additional information:

  1. PUT _ingest/pipeline/my-json-pipeline
  2. {
  3. "description": "Example pipeline using the JsonProcessor",
  4. "processors": [
  5. {
  6. "json": {
  7. "field": "raw_data",
  8. "target_field": "parsed_data"
  9. "on_failure": [
  10. {
  11. "set": {
  12. "field": "error_message",
  13. "value": "Failed to parse JSON data"
  14. }
  15. },
  16. {
  17. "fail": {
  18. "message": "Failed to process JSON data"
  19. }
  20. }
  21. ]
  22. }
  23. },
  24. {
  25. "set": {
  26. "field": "processed_timestamp",
  27. "value": ""
  28. }
  29. }
  30. ]
  31. }

copy

Step 2 (Optional): Test the pipeline

It is recommended that you test your pipeline before you ingest documents.

To test the pipeline, run the following query:

  1. POST _ingest/pipeline/my-json-pipeline/_simulate
  2. {
  3. "docs": [
  4. {
  5. "_source": {
  6. "raw_data": "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}"
  7. }
  8. },
  9. {
  10. "_source": {
  11. "raw_data": "{\"name\":\"Jane\",\"age\":25,\"city\":\"Los Angeles\"}"
  12. }
  13. }
  14. ]
  15. }

copy

Response

The following example response confirms that the pipeline is working as expected:

  1. {
  2. "docs": [
  3. {
  4. "doc": {
  5. "_index": "_index",
  6. "_id": "_id",
  7. "_source": {
  8. "processed_timestamp": "2024-05-30T15:24:48.064472090Z",
  9. "raw_data": """{"name":"John","age":30,"city":"New York"}""",
  10. "parsed_data": {
  11. "name": "John",
  12. "city": "New York",
  13. "age": 30
  14. }
  15. },
  16. "_ingest": {
  17. "timestamp": "2024-05-30T15:24:48.06447209Z"
  18. }
  19. }
  20. },
  21. {
  22. "doc": {
  23. "_index": "_index",
  24. "_id": "_id",
  25. "_source": {
  26. "processed_timestamp": "2024-05-30T15:24:48.064543006Z",
  27. "raw_data": """{"name":"Jane","age":25,"city":"Los Angeles"}""",
  28. "parsed_data": {
  29. "name": "Jane",
  30. "city": "Los Angeles",
  31. "age": 25
  32. }
  33. },
  34. "_ingest": {
  35. "timestamp": "2024-05-30T15:24:48.064543006Z"
  36. }
  37. }
  38. }
  39. ]
  40. }

copy

Step 3: Ingest a document

The following query ingests a document into an index named my-index:

  1. POST my-index/_doc?pipeline=my-json-pipeline
  2. {
  3. "raw_data": "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}"
  4. }

copy

Response

The response confirms that the document containing the JSON data from the raw_data field was successfully indexed:

  1. {
  2. "_index": "my-index",
  3. "_id": "mo8yyo8BwFahnwl9WpxG",
  4. "_version": 1,
  5. "result": "created",
  6. "_shards": {
  7. "total": 2,
  8. "successful": 1,
  9. "failed": 0
  10. },
  11. "_seq_no": 3,
  12. "_primary_term": 2
  13. }

copy

Step 4 (Optional): Retrieve the document

To retrieve the document, run the following query:

  1. GET my-index/_doc/1

copy