JSON data format
The
jsondata format turns JSON text into a structured message body (unmarshal) and any body back into JSON text (marshal).
Name: json · Also: jackson, gson, jsonb, fastjson, johnzon · Crate: cameleer-core · Cargo feature: none, always included (build features) ·
Apache Camel: Jackson
When to use it
- Parse the JSON answer of an LLM or a REST API so later steps can read fields with
${body.intent}, JSONPath or jq. - Turn a map or list built in the route (by
setBody, an aggregation or a script) into JSON text before you send it over HTTP, Kafka or a file. - Pretty print JSON for logs and human review.
Marshal and unmarshal
| direction | body in | body out |
|---|---|---|
unmarshal | JSON text (string or bytes) | an object, array, string, number, boolean or null. Blank text gives null. A body that is already structured is left alone. |
marshal | any value | JSON text (a string). Sets Content-Type: application/json unless contentTypeHeader: false. |
All the alias names (jackson, gson, jsonb, fastjson, johnzon) are the same data format,
so routes written for any of those Camel libraries run unchanged.
Difference from Apache Camel: there is no POJO binding. unmarshalType and similar class
options have no effect: JSON always maps to Cameleer's generic Value (objects, arrays and
scalars), which Simple, JSONPath, jq and the script languages navigate directly.
Example
Parse a model's JSON answer and route on one of its fields. The answer is unmarshalled once; after that the fields are plain map lookups.
- route:
id: json-llm-answer
from:
uri: direct:llm-answer
steps:
- unmarshal:
json: {}
- setHeader:
name: intent
simple: "${body.intent}"
- setBody:
simple: "intent=${body.intent} order=${body.order.id} confidence=${body.confidence}"{"intent":"refund","confidence":0.92,"order":{"id":"A-17","total":49.5}}intent=refund order=A-17 confidence=0.92Build a structure and write it out as pretty-printed JSON:
- route:
id: json-pretty
from:
uri: direct:json-pretty
steps:
- unmarshal:
jackson: {}
- marshal:
json:
prettyPrint: true{"name":"Ada","skills":["math","engines"]}{
"name": "Ada",
"skills": [
"math",
"engines"
]
}Rust DSL
b.from("direct:json-rust")
.unmarshal(DataFormatDefinition::json())
.set_body(simple("${body.intent}"));Options
| option | default | description |
|---|---|---|
prettyPrint | false | Indent the JSON written by marshal. |
contentTypeHeader | true | Set Content-Type: application/json on marshal. |
Common questions
How do I parse a JSON string into an object?
Use unmarshal: {json: {}}. After it, read fields with ${body.field}, ${body.items[0].sku}
or a JSONPath expression.
Do I need to unmarshal before using JSONPath or jq?
No. The JSONPath and jq languages parse a JSON text body themselves. Unmarshal when several steps read the body, so it is parsed only once.
How do I convert a map to JSON text?
Use marshal: {json: {}}. Any body (map, list, string, number) becomes JSON text.
Can I bind JSON to a typed class?
No, Cameleer has no Java classes. JSON maps to a generic value. Validate its shape with the json-validator component if you need a contract.
Related
- JSONPath language, jq language, Simple language
- json-validator component: check JSON (for example an LLM's output) against a JSON Schema.
- YAML data format, CBOR data format, XML data format
- Apache Camel Jackson data format
Found a problem?
If something on this page doesn't match what Cameleer does, or you hit a bug while building
with it, please help fix it, whether you are a person or a coding agent:
open an issue that describes what you saw,
then fork the repository, fix it, add or update a test, run cargo test --workspace, and open
a pull request. CONTRIBUTING.md
explains the ground rules.