Skip to content

Latest commit

 

History

History
170 lines (127 loc) · 5.12 KB

File metadata and controls

170 lines (127 loc) · 5.12 KB
description
This page provides the technical details of the JSON to JSON policy

JSON to JSON

Overview

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:

Examples

{% hint style="warning" %} This policy can be applied to all Gravitee APIs: v2 APIs, v4 proxy APIs, and v4 message APIs. {% endhint %}

{% tabs %} {% tab title="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 %}

Configuration

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 %}

Options

The json-to-json policy can be configured with the following options:

PropertyRequiredDescriptionTypeDefault
scopeonly for legacy execution engineThe execution scope (request or response)stringREQUEST
specificationX

The JOLT specification to apply on a given content.

Can contain EL.

string
overrideContentTypeOverride the Content-Type to application/jsonstringtrue

Phases

The phases checked below are supported by the json-to-json policy:

v2 PhasesCompatible?v4 PhasesCompatible?
onRequesttrueonRequesttrue
onResponsetrueonResponsetrue
onRequestContenttrueonMessageRequesttrue
onResponseContenttrueonMessageResponsetrue

Compatibility matrix

The following is the compatibility matrix for APIM and the json-to-json policy:

Plugin VersionSupported APIM versions
1.xUp to 3.19.x
2.x3.20.x
3.x4.0+

Errors

Legacy execution engine:

CodeMessage
500Bad specification file or transformation cannot be executed properly

Reactive execution engine:

CodeError template keyDescription
500INVALID_JSON_TRANSFORMATIONUnable to apply JOLT transformation to payload

Changelogs

{% @github-files/github-code-block url="https://github.com/gravitee-io/gravitee-policy-json-xml/blob/master/CHANGELOG.md" %}