JSONata
Since Camel 3.5
Only producer is supported
The Jsonata component allows you to process JSON messages using the JSONATA specification. This can be ideal when doing JSON to JSON transformation and other transformations from JSON.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jsonata</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> URI format
jsonata:specName[?options]
Where specName is the classpath-local URI of the specification to invoke; or the complete URL of the remote specification (e.g.: file://folder/myfile.vm).
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The JSONata component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
Whether the endpoints take the JSONata expression from the CamelJsonataResourceUri header, when the header is set, instead of from their resource. It is the default of the allowTemplateFromHeader option of each endpoint. Enable it only when the header comes from a trusted source. | false | boolean | |
Whether the endpoints keep the content of their resource in memory after the first load, instead of reading it again for each exchange. It is the default of the contentCache option of each endpoint. | true | boolean | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
A JsonataFrameBinding that binds custom functions and variables to the Jsonata.Frame of each evaluation. It is used by the endpoints that do not set their own frameBinding option. | JsonataFrameBinding |
Endpoint Options
The JSONata endpoint is configured using URI syntax:
jsonata:resourceUri
With the following path and query parameters:
Path Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Required Path to the resource. You can prefix with: classpath, file, http, ref, or bean. classpath, file and http loads the resource using these protocols (classpath is default). ref will lookup the resource in the registry. bean will call a method on a bean to be used as the resource. For bean you can specify the method name after dot, eg bean:myBean.myMethod. | String |
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Deprecated Not used by jsonata. Deprecation note: The jsonata component builds no context map, so this option has no effect. | false | boolean | |
Whether the producer takes the JSONata expression from the CamelJsonataResourceUri header, when the header is set, instead of from the resourceUri option. Enable it only when the header comes from a trusted source, as its value can be any expression or the URI of any resource that Camel can read. | false | boolean | |
Sets whether to use resource content cache or not. | true | boolean | |
How the producer reads the message body as the input of the expression. With Jackson, a body that is JSON text (its first non-whitespace character is \{ or [), bytes, a stream or a file is parsed as JSON, other text is a JSON string value, and any other body, such as a Map or a List, is converted by Jackson. With JsonString, the body is always parsed as JSON. Enum values:
| Jackson | JsonataInputOutputType | |
The type of the result that the producer sets as the message body: Jackson for a Jackson 3 JsonNode, or JsonString for the JSON text. Enum values:
| Jackson | JsonataInputOutputType | |
Whether to format the JSON text of the result with indentation and line breaks. It applies only when outputType is JsonString. | false | boolean | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
A JsonataFrameBinding that the producer calls with the Jsonata.Frame of each evaluation, to bind custom functions and variables that the expression can use. If not set, the frameBinding option of the component is used. | JsonataFrameBinding |
Message Headers
The JSONata component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelJsonataResourceUri (producer) Constant: | The JSONata expression to use instead of the resource of the endpoint, or the URI of a resource that holds it, with a scheme such as classpath:, file: or http:. The producer reads it only when allowTemplateFromHeader is true, and removes it from the message. | String |
Message Body
The message body at each side of the JSONata component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| JSON data: a Map, a List, a JsonNode or another object Jackson converts, or JSON as text, bytes, a stream or a file, which is parsed. Text that does not start with { or [ is read as one JSON string. With inputType=JsonString the body is always parsed as JSON.
|
Producer output |
| The result as a Jackson 3 JsonNode, or as JSON text with outputType=JsonString.
|
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Usage
Input and output types
With the default inputType, Jackson, the body can be:
-
JSON text (a
Stringor other text whose first non-whitespace character is{or[), which is parsed as JSON; -
bytes, an
InputStream(including a stream cache) or a file (java.io.File,java.nio.file.Pathor the file of the File component), which is parsed as JSON. The text is decoded in the charset of the exchange (Exchange.CHARSET_NAME) when one is set; otherwise Jackson detects the encoding; -
any other object, such as a
Map, aListor a JacksonJsonNode(Jackson 3 or Jackson 2), which is converted by Jackson.
Text that does not start with { or [ is a JSON string value, so for example the expression $uppercase($) turns the body hello into "HELLO". Text that starts with { or [ but is not valid JSON fails with the parse error.
With inputType=JsonString, the body is always parsed as JSON.
The output is a Jackson 3 JsonNode (tools.jackson.databind.JsonNode) by default; set outputType=JsonString to get a JSON String.
Remote expressions
The expression can be loaded from an http or https URL. The options of the endpoint, such as inputType, outputType and prettyPrint, are used by the endpoint, and only the other query parameters are sent to the server. For example, jsonata:https://example.com/transform.jsonata?version=2&outputType=JsonString requests https://example.com/transform.jsonata?version=2.
Examples
Basic
For example, you could use something like:
The messages of the queue are JSON text, so the examples set inputType and outputType to JsonString: the body is always parsed as JSON, and the result is JSON text. With the default, Jackson, the result is a JsonNode.
-
Java
-
XML
-
YAML
from("activemq:My.Queue")
.to("jsonata:com/acme/MyResponse.json?inputType=JsonString&outputType=JsonString"); <route>
<from uri="activemq:My.Queue"/>
<to uri="jsonata:com/acme/MyResponse.json?inputType=JsonString&outputType=JsonString"/>
</route> - route:
from:
uri: activemq:My.Queue
steps:
- to:
uri: jsonata:com/acme/MyResponse.json
parameters:
inputType: JsonString
outputType: JsonString And a file-based resource:
-
Java
-
XML
-
YAML
from("activemq:My.Queue")
.to("jsonata:file://myfolder/MyResponse.json?contentCache=true&inputType=JsonString&outputType=JsonString")
.to("activemq:Another.Queue"); <route>
<from uri="activemq:My.Queue"/>
<to uri="jsonata:file://myfolder/MyResponse.json?contentCache=true&inputType=JsonString&outputType=JsonString"/>
<to uri="activemq:Another.Queue"/>
</route> - route:
from:
uri: activemq:My.Queue
steps:
- to:
uri: jsonata:file://myfolder/MyResponse.json
parameters:
contentCache: true
inputType: JsonString
outputType: JsonString
- to:
uri: activemq:Another.Queue Frame bindings
It is possible to configure custom functions that can be called from Jsonata. For example you might want to be able to inject environment variables:
-
Java
-
XML
-
YAML
from("activemq:My.Queue")
.to("jsonata:file://myfolder/MyResponse.json?contentCache=true&inputType=JsonString&outputType=JsonString&frameBinding=#customBindings")
.to("activemq:Another.Queue"); <route>
<from uri="activemq:My.Queue"/>
<to uri="jsonata:file://myfolder/MyResponse.json?contentCache=true&inputType=JsonString&outputType=JsonString&frameBinding=#customBindings"/>
<to uri="activemq:Another.Queue"/>
</route> - route:
from:
uri: activemq:My.Queue
steps:
- to:
uri: jsonata:file://myfolder/MyResponse.json
parameters:
contentCache: true
inputType: JsonString
outputType: JsonString
frameBinding: "#customBindings"
- to:
uri: activemq:Another.Queue A custom binding might look like the following:
@NoArgsConstructor
public class CustomJsonataFrameBinding implements JsonataFrameBinding {
@Override
public void bindToFrame(Jsonata.Frame frame) {
frame.bind("env", (String s) -> System.getenv(s));
}
}