Write JSON processor
출처: 문서
본문
Write JSON 프로세서
write_json 프로세서는 이벤트의 객체를 JSON 문자열로 변환해요. 소스 및 대상 필드 이름을 선택하도록 프로세서를 사용자 지정할 수 있어요.
| 옵션 | 설명 | 예시 |
|---|---|---|
source |
필수 필드로, 이벤트에서 구문 분석할 메시지나 객체가 포함된 필드의 이름을 지정해요. | source가 "message"로 설정되고 입력이 {"message": {"key1":"value1", "key2":{"key3":"value3"}}}인 경우, write_json 프로세서는 이벤트를 "{\"key1\":\"value1\",\"key2\":{\"key3\":\"value3\"}}"로 출력해요. |
target |
선택적 필드로, 결과 JSON 문자열이 저장될 필드의 이름을 지정해요. target이 지정되지 않으면 source 필드가 사용돼요. |
key1 |
예시
다음 예제는 write_json을 두 번 사용해요. 먼저 details 객체를 target이라는 새 JSON 문자열 필드로 복사하고, 그다음 target이 생략될 때 원래 payload 필드를 덮어써요.
write-json-demo-pipeline:
source:
http:
path: /logs
ssl: false
processor:
# 1) Copy the nested "details" object into a JSON string at "details_json"
- write_json:
source: details
target: details_json
# 2) Overwrite "payload" with its JSON-string representation
- write_json:
# no target -> result stored back into "payload"
source: payload
sink:
- opensearch:
hosts: ["https://opensearch:9200"]
insecure: true
username: admin
password: admin_pass
index_type: custom
index: write-json-demo-%{yyyy.MM.dd}
{% include copy.html %}
다음 명령을 사용하여 이 파이프라인을 테스트할 수 있어요.
curl -sS -X POST "http://localhost:2021/logs" \
-H "Content-Type: application/json" \
-d '[
{
"message": "order created",
"details": {"order_id": 123, "items": [{"sku": "A1", "qty": 2}], "expedited": true},
"payload": {"user": {"id": "u-42", "role": "admin"}, "ip": "10.0.0.5"}
},
{
"message": "order updated",
"details": {"order_id": 124, "items": [{"sku": "B9", "qty": 1}], "expedited": false},
"payload": {"user": {"id": "u-77", "role": "viewer"}, "ip": "10.0.0.9"}
}
]'
{% include copy.html %}
OpenSearch에 저장된 문서에는 다음 정보가 포함돼요.
{
...
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "write-json-demo-2025.10.15",
"_id" : "YMQA6ZkB1u9wkbZgz8wu",
"_score" : 1.0,
"_source" : {
"message" : "order created",
"details" : {
"order_id" : 123,
"items" : [
{
"sku" : "A1",
"qty" : 2
}
],
"expedited" : true
},
"payload" : "{\"user\":{\"id\":\"u-42\",\"role\":\"admin\"},\"ip\":\"10.0.0.5\"}",
"details_json" : "{\"order_id\":123,\"items\":[{\"sku\":\"A1\",\"qty\":2}],\"expedited\":true}"
}
},
{
"_index" : "write-json-demo-2025.10.15",
"_id" : "YcQA6ZkB1u9wkbZgz8wu",
"_score" : 1.0,
"_source" : {
"message" : "order updated",
"details" : {
"order_id" : 124,
"items" : [
{
"sku" : "B9",
"qty" : 1
}
],
"expedited" : false
},
"payload" : "{\"user\":{\"id\":\"u-77\",\"role\":\"viewer\"},\"ip\":\"10.0.0.9\"}",
"details_json" : "{\"order_id\":124,\"items\":[{\"sku\":\"B9\",\"qty\":1}],\"expedited\":false}"
}
}
]
}
}