Overview
The pfx-json component provides JSON marshalling, unmarshalling, and structural transformation. Transformation uses the JOLT library -- a JSON-to-JSON transform engine that maps fields, renames keys, and restructures arrays via a declarative specification.
URI pattern: pfx-json:method[?options]
Methods
|
Method |
Description |
|---|---|
|
|
Serialize body (Map, List, POJO) to a JSON string |
|
|
Parse a JSON string in the body to a Map/List |
|
|
Apply a JOLT specification to restructure the JSON body |
|
|
Remap JSON fields using a mapper definition |
Parameters
transform
|
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
String |
-- |
JOLT spec source: inline JSON string, resource path ( |
|
|
String |
|
JOLT input type ( |
|
|
String |
|
JOLT output type ( |
remap
|
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
String |
-- |
Bean ID of the mapper definition |
Examples
Parse JSON response from external API
<routes xmlns="http://camel.apache.org/schema/spring">
<route id="fetchAndTransform">
<from uri="timer://fetch?repeatCount=1"/>
<to uri="pfx-rest:get?url={{api.url}}/products"/>
<to uri="pfx-json:unmarshal"/>
<!-- body is now a List of Maps -->
<split>
<simple>${body}</simple>
<to uri="pfx-api:integrate?objectType=P&mapper=productMapper"/>
</split>
</route>
</routes>
Transform with JOLT specification
<!-- Reshape the JSON structure before loading -->
<to uri="pfx-json:transform?specification=classpath:jolt/product-transform.json"/>
<to uri="pfx-json:unmarshal"/>
Example JOLT spec (resources/jolt/product-transform.json):
[{
"operation": "shift",
"spec": {
"items": {
"*": {
"id": "sku",
"name": "label",
"price": "attribute1"
}
}
}
}]
Serialize to JSON for outbound REST call
<to uri="pfx-api:fetch?objectType=P&filter=allProducts"/>
<to uri="pfx-json:marshal"/>
<to uri="pfx-rest:post?url={{api.url}}/import&contentType=application/json"/>
Common Pitfalls
-
unmarshalproduces aMapfor JSON objects and aListof Maps for JSON arrays. Use<split><simple>${body}</simple>to iterate arrays. -
JOLT specs are case-sensitive -- field names must exactly match the input JSON keys.
-
For
transform, thespecificationis evaluated once at startup if it is a static string. Usefile:orclasspath:for specs stored in theresources/directory. Useheader.nameorproperty.namefor dynamic specs resolved at runtime per exchange. -
marshal/unmarshaldo not perform type conversion -- dates remain strings unless a mapper converter is applied downstream.