JSON
JSON
JSON
• 4.1.0
• 4.0.0
• 3.2.0
• 3.1.0
• 3.0.0
• Show All
SearchK
• Home
• Get Started
• Design APIs
• Consume APIs
• Integrate
• Streaming
• Analytics
• Observe
• Reference
• Administer
• Install and Setup
• Tutorials
Report Issues
•
• Reference
o Product REST APIs
o Product Configurations
o API Controller (APICTL)
o API Kubernetes Operator
o Connectors
o Streaming Connectors
o Integration Artifacts
▪ REST API Properties
▪ Proxy Service Properties
▪ Mediator Catalog
▪ About Mediators
▪ Aggregate Mediators
▪ Builder Mediator
▪ Cache Mediator
▪ Call Mediator
▪ Call Template Mediator
▪ Callout Mediator
▪ Class Mediator
▪ Clone Mediator
▪ Data Mapping
▪ Data Service Call Mediator
▪ DBLookup Mediator
▪ DB Report Mediator
▪ Drop Mediator
▪ EJB Mediator
▪ Enrich Mediator
▪ Entitlement Mediator
▪ FastXSLT Mediator
▪ Fault Mediator
▪ Filter Mediator
▪ ForEach Mediator
▪ Header Mediator
▪ Iterate Mediator
▪ JSON Transform Mediator
▪ Log Mediator
▪ Loopback Mediator
▪ NTLM Mediator
▪ OAuth Mediator
▪ PayloadFactory Mediator
▪ Property Mediator
▪ Property Group Mediator
▪ Respond Mediator
▪ Script Mediator
▪ Send Mediator
▪ Sequence Mediator
▪ Smooks Mediator
▪ Store Mediator
▪ Switch Mediator
▪ Throttle Mediator
▪ Transaction Mediator
▪ URLRewrite Mediator
▪ Validate Mediator
▪ XQuery Mediator
▪ XSLT Mediator
▪ Property Catalog
▪ Scheduled Task Properties
▪ Synapse Properties
▪ Inbound Endpoints
▪ Message Stores and Processors
▪ Templates
▪ Endpoints
▪ Data Services
▪ Transport Configs
▪ Micro Integrator Security References
o Customizations
o Best Practices
o Accessibility Compliance
o Guides
o Troubleshooting
o FAQ
Those configurations are applied globally and you cannot have independent configurations for each mediation scenario. With JSON
Transform mediator you can define the properties inside the mediation and control the transformation independently. Also you can have
a JSON schema to correct the payload if there are inconsistencies in the transformation.
Info
Syntax¶
<jsontransform [schema="string"]>
<property name="string" value="string"/>*
</jsontransform>
Configuration¶
The general parameters available for configuring the JSON Transform mediator are as follows.
Parameter Description
Name
Schema
This parameter is used for specifying the registry location of the JSON schema file. You can
Local Entry as well
Apart from defining a schema, you can also add properties to control XML to JSON transformation. The parameters available for
configuring a property are as follows:
Parameter Description
Name
Schema
The name of the property that needs to be overridden in the sequence. The JSON Transform
mediator supports only the parameters related to XML to JSON conversion. The list of propert
supported can be found here.
Property
Value The value that should be overridden.
<books>
<book>Harry Potter</book>
<book>Lord of the Rings</book>
</books>
{"books" : { "book" : ["Harry Potter", "Lord of the Rings"]}}
Let's say we get only one result. The converted JSON would come out like below.
<books>
<book>Harry Potter</book>
</books>
{"books" : { "book" : "Harry Potter"}}
Theoretically the above conversion is correct. However, a client might expect an array and not a string.
We can tackle the above issue with a JSON Schema and correct the output to be an JSON Array.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"books": {
"type": "object",
"properties": {
"book": {
"type": "array"
}
}
}
}
}
<person>
<id>56783</id>
<name>Alice</name>
<isAdmin>true</isAdmin>
</person>
By default, the JSON output would look like below after the mediation.
{
"person": {
"id": 56783,
"name": "Alice",
"isAdmin": true
}
}
The field id has been converted to number, name to String and isAdmin to boolean.
The runtime has automatically detected and parsed the values to native data-types. But there might be a scenario where the client
expects a String type for id.
We want the native conversion rules applied to name and isAdmin fields and not id.
With JSON Transform mediator, we can use a JSON schema to tackle this issue.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"person": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
}
}
}
}
With this schema correction, the JSON payload would come out as below. This gives granular level control over individual field data
types rather than using a global property.
{
"person": {
"id": "56783",
"name": "Alice",
"isAdmin": true
}
}
Example¶
Given below is a sample schema file (Schema.json) file that you can use for running the examples given below. Add this sample
schema file (i.e. Schema.json) to the following registry path: conf:/Schema.json. For instructions on adding the schema file to the
Registry Resources Project, see Creating Registry Resource.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"fruit": {
"type": "string",
"minLength": 4,
"maxLength": 6,
"pattern": "^[0-9]{1,45}$"
},
"price": {
"type": "number",
"minimum": 2,
"maximum": 20,
"exclusiveMaximum": 20,
"multipleOf": 2.5
}
},
"required": [
"price"
]
}
<jsonObject>
<fruit>12345</fruit>
<price>7.5</price>
<quantity>10</quantity>
</jsonObject>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="transformMediatorSimpleAutoPrimitive" startOnLoad="true"
transports="https http">
<description/>
<target>
<inSequence>
<property name="messageType" scope="axis2"
value="application/json"/>
<jsontransform>
<property
name="synapse.commons.json.output.autoPrimitive" value = "false"/>
</jsontransform>
<respond/>
</inSequence>
</target>
</proxy>
Output: All the numeric values have been converted to string values since the auto primitive property is defined as false.
{
"fruit": "12345",
"price": "7.5",
"quantity": "10"
}
Output: The 'fruit' value has been converted to string and the 'price' value has been converted to a number according to the schema
definition. Please note that 'quantity' has been converted to a number because it is the default property according to
the synapse.properties file.
{
"fruit": "12345",
"price": 7.5,
"quantity": 10
}
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="transformMediatorPropertyAndSchema" startOnLoad="true"
transports="https http">
<description/>
<target>
<inSequence>
<property name="messageType" scope="axis2"
value="application/json"/>
<jsontransform schema="conf:/Schema.json">
<property
name="synapse.commons.json.output.autoPrimitive" value = "false"/>
</jsontransform>
<respond/>
</inSequence>
</target>
</proxy>
Output: The 'fruit' value has been converted to a string and the 'price' value has been converted to a number according to the schema
definition. Please note that 'quantity' has been converted to a string because we have overridden the global synapse.properties file.
{
"fruit": "12345",
"price": 7.5,
"quantity": "10"
}
Top
PreviousIterate Mediator
NextLog Mediator