Skip to content

JSON

Inbuilt JSON marshalling component: Transforms to and from JSON using the ultra-fast Jackson library

For more information about json-jackson: https://camel.apache.org/components/latest/dataformats/json-jackson-dataformat.html.

Example

Transforms the Log object emitted by the web3j component to a JSON string:

import org.apache.camel.model.dataformat.JsonLibrary

def ethClientURL = p6.appconfig.get('ethClientURL')
def contractAddress =  p6.appconfig.get('contractAddress')

p6.camel.getCtx().addRoutes(new RouteBuilder() {
    void configure() {
    from("web3j://" + ethClientURL + "?address=" + contractAddress + "&operation=ETH_LOG_FLOWABLE")
        .marshal().json(JsonLibrary.Jackson)
        .setHeader("Content-Type", constant("application/json; charset=utf-8"))
        .to("p6cmb://scripts?platform6.request.action=execute&id=p6_demo.BlockchainEventHandler")
        .routeId("p6_demo Web3j - BlockchainEventHandler")
        .description("p6_demo Blockchain Event Handler")
    }
})

Warning

If you want to read the result of this JSON marshalling component transformation using the pipeline (in a script for example) you need to set the contentType header with a charset (as shown before). By default, the result of the transformation does not contain the charset.