pfx-json Component

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

marshal

Serialize body (Map, List, POJO) to a JSON string

unmarshal

Parse a JSON string in the body to a Map/List

transform

Apply a JOLT specification to restructure the JSON body

remap

Remap JSON fields using a mapper definition


Parameters

transform

Parameter

Type

Default

Description

specification

String

--

JOLT spec source: inline JSON string, resource path (classpath:, file:), exchange header (header.myHeader), or exchange property (property.myProp)

inputType

String

Hydrated

JOLT input type (Hydrated, JsonString)

outputType

String

Hydrated

JOLT output type (Hydrated, JsonString)

remap

Parameter

Type

Default

Description

mapper

String

--

Bean ID of the mapper definition


Examples

Parse JSON response from external API

XML
<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&amp;mapper=productMapper"/>
        </split>
    </route>
</routes>

Transform with JOLT specification

XML
<!-- 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):

JSON
[{
  "operation": "shift",
  "spec": {
    "items": {
      "*": {
        "id": "sku",
        "name": "label",
        "price": "attribute1"
      }
    }
  }
}]

Serialize to JSON for outbound REST call

XML
<to uri="pfx-api:fetch?objectType=P&amp;filter=allProducts"/>
<to uri="pfx-json:marshal"/>
<to uri="pfx-rest:post?url={{api.url}}/import&amp;contentType=application/json"/>

Common Pitfalls

  • unmarshal produces a Map for JSON objects and a List of 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, the specification is evaluated once at startup if it is a static string. Use file: or classpath: for specs stored in the resources/ directory. Use header.name or property.name for dynamic specs resolved at runtime per exchange.

  • marshal / unmarshal do not perform type conversion -- dates remain strings unless a mapper converter is applied downstream.