description |
---|
This page provides the technical details of the JSON to JSON policy |
You can use the json-to-json
policy to apply a transformation (or mapping) on the request and/or response and/or message content.
This policy is based on the JOLT library.
In APIM, you need to provide the JOLT specification in the policy configuration.
{% hint style="info" %} You can use APIM EL in the JOLT specification. {% endhint %}
At request/response level, the policy will do nothing if the processed request/response does not contain JSON. This policy checks the Content-Type
header before applying any transformation.
At message level, the policy will do nothing if the processed message has no content. It means that the message will be re-emitted as is.
Functional and implementation information for the json-to-json
policy is organized into the following sections:
{% hint style="warning" %} This policy can be applied to v2 APIs, v4 HTTP proxy APIs, and v4 message APIs. It cannot be applied to v4 TCP proxy APIs. {% endhint %}
{% tabs %} {% tab title="HTTP proxy API example" %} For this input:
{
"_id": "57762dc6ab7d620000000001",
"name": "name",
"__v": 0
}
And this JOLT specification:
[
{
"operation": "shift",
"spec": {
"_id": "id",
"*": {
"$": "&1"
}
}
},
{
"operation": "remove",
"spec": {
"__v": ""
}
}
]
The output is as follows:
{
"id": "57762dc6ab7d620000000001",
"name": "name"
}
{% endtab %}
{% tab title="Message API example" %} For this input:
{
"_id": "57762dc6ab7d620000000001",
"name": "name",
"__v": 0
}
And this JOLT specification:
[
{
"operation": "shift",
"spec": {
"_id": "id",
"*": {
"$": "&1"
}
}
},
{
"operation": "remove",
"spec": {
"__v": ""
}
}
]
The output is as follows:
{
"id": "57762dc6ab7d620000000001",
"name": "name"
}
{% endtab %} {% endtabs %}
Sample policy configuration is shown below:
{% code title="Sample Configuration" %}
{
"json-to-json": {
"scope": "REQUEST",
"specification": "[{ \"operation\": \"shift\", \"spec\": { \"_id\": \"id\", \"*\": { \"$\": \"&1\" } } }, { \"operation\": \"remove\", \"spec\": { \"__v\": \"\" } }]"
}
}
{% endcode %}
The json-to-json
policy can be configured with the following options:
Property | Required | Description | Type | Default |
---|---|---|---|---|
scope | only for legacy execution engine | The execution scope (request or response ) | string | REQUEST |
specification | X | The JOLT specification to apply on a given content. Can contain EL. | string | |
overrideContentType | Override the Content-Type to application/json | string | true |
The phases checked below are supported by the json-to-json
policy:
v2 Phases | Compatible? | v4 Phases | Compatible? |
---|---|---|---|
onRequest | true | onRequest | true |
onResponse | true | onResponse | true |
onRequestContent | true | onMessageRequest | true |
onResponseContent | true | onMessageResponse | true |
The following is the compatibility matrix for APIM and the json-to-json
policy:
Plugin Version | Supported APIM versions |
---|---|
1.x | Up to 3.19.x |
2.x | 3.20.x |
3.x | 4.0+ |
Legacy execution engine:
Code | Message |
---|---|
500 | Bad specification file or transformation cannot be executed properly |
Reactive execution engine:
Code | Error template key | Description |
---|---|---|
500 | INVALID_JSON_TRANSFORMATION | Unable to apply JOLT transformation to payload |
{% @github-files/github-code-block url="https://github.com/gravitee-io/gravitee-policy-json-xml/blob/master/CHANGELOG.md" %}