Skip to content

gravitee-io/gravitee-policy-json-to-json

Repository files navigation

JSON to JSON transformation policy

Gravitee.io License Releases CircleCI

Phases

V3 engine

onRequestContent onResponseContent

X

X

V4 engine

onRequest onResponse onMessageRequest onMessageResponse

X

X

X

X

Description

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.

Note
You can use APIM EL in the JOLT specification.

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.

Compatibility with APIM

Plugin version

APIM version

1.x

Up to 3.19.x

2.x

3.20.x

3.x

4.x to latest

Configuration

You can configure the policy with the following options:

Property Required Description Type Default

scope

only for v3 engine

The execution scope (request or response)

string

REQUEST

specification

X

The JOLT specification to apply on a given content.

Can contains EL.

string

overrideContentType

Override the Content-Type to application/json.

string

true

Example configuration:

{
    "json-to-json": {
        "scope": "REQUEST",
        "specification": "[{ \"operation\": \"shift\", \"spec\": { \"_id\": \"id\", \"*\": { \"$\": \"&1\" } } }, { \"operation\": \"remove\", \"spec\": { \"__v\": \"\" } }]"
    }
}

Examples

For this input:

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

Errors

V3 engine

Code Message

500

Bad specification file or transformation cannot be executed properly

V4 engine

Phase Code Error template key Description

*

500

INVALID_JSON_TRANSFORMATION

Unable to apply JOLT transformation to payload