Skip to main content

Conditional JSON Patch

Javascript Object Notation (JSON) Patch is a JSON document structure for expressing a sequence of operations to apply to a JSON document. In some cases, it is desirable to conditionally apply a patch to a JSON data message payload based on the value of a given field in the document. The test operation can be used to conditionally apply a patch to a data message payload.

This example demonstrates using a test operation with the json-patch transformer to only apply a patch to payloads for a specific device. The device identifier is accessed by injecting it into the data message payload using the inject-metadata transformer. A subsequent json-patch step removes the metadata from the payload.

filter:
path: "*"
content_type: application/json
steps:
- name: get-metadata
transformer:
type: inject-metadata
version: v1
- name: conditional-patch
transformer:
type: json-patch
version: v1
parameters:
patch: |
[
{"op": "test", "path": "/device_id", "value": "649998262fecb43eb2d39859"},
{"op": "add", "path": "/demo", "value": true}
]
- name: remove-metadata
transformer:
type: json-patch
version: v1
parameters:
patch: |
[
{"op": "remove", "path": "/device_id"},
{"op": "remove", "path": "/project_id"},
{"op": "remove", "path": "/timestamp"}
]
- name: send-lightdb
destination:
type: lightdb-stream
version: v1