Data Transformation and Validation

CSV

  • Description: the CSV component is used to read and write data in CSV (Comma-Separated Values) format. It simplifies the handling of CSV files, enabling efficient data conversions and transformations.

CSV to JSON

  • Example

    • The route is triggered when a POST request is made to the /csv-to-json endpoint.

    • The data in the request body, in CSV format, is deserialized (unmarshal) into maps (useMaps: true) using the defined delimiter (delimiter: ",").

    • These maps are transformed into JSON and returned as the response.

Script

Diagram

- from:
    uri: "rest:POST:/csv-to-json"
    steps:
    - unmarshal:
        csv:
          delimiter: ","
          useMaps: true
    - marshal:
        json: {}
component csv to json

JSON to CSV

  • Example

    • The flow starts with a POST request to the /json-to-csv endpoint with data in JSON format.

    • The received JSON is converted (unmarshal) into an internal structure that can be manipulated (maps).

    • The processed data is converted into CSV format (marshal).

      • Additionally, the delimiter ; (semicolon) is used to separate values and

      • The headers firstName and lastName are added.

Script

Diagram

- from:
    uri: "rest:POST:/json-to-csv"
    steps:
    - unmarshal:
        json: {}
    - marshal:
        csv:
          delimiter: ";"
          header:
          - firstName
          - lastName
component json to csv
See more information about Data Format as a Camel component.

JOLT

  • Description: the JOLT allows for transforming and manipulating JSON data declaratively, using specifications defined in JOLT files.

  • Example

    • The flow starts with a POST request at the endpoint /jolt-poc.

    • The request body is logged.

    • The flow transforms the request body using a Jolt template located in the template.json file.

      • The parameters inputType: JsonString and outputType: JsonString ensure that the input and output are handled as JSON strings.

      • contentCache: true enables caching of the results.

    • Finally, the flow logs the result of the transformation.

Script

Diagram

- from:
    uri: "rest:post:/jolt-poc"
    steps:
        - log:
            message: "Got body: ${body}"
        - to:
            uri: "jolt:template.json"
            parameters:
                inputType: JsonString
                outputType: JsonString
                contentCache: true
        - log:
            message: "Result: ${body}"
component jolt

JSLT

  • Description: the JSLT component is used to transform data using templates written in JSLT. It processes input information, such as JSON or XML, reorganizing and formatting it according to a model that you define.

  • Example

    • The flow starts with a POST request at the endpoint /hello.

    • The request data is transformed using the JSLT component with the template file expression.jslt.

      • The parameter contentCache=true enables caching of the template content.

    • Finally, the transformed message is logged at the info level.

Script

Diagram

- from:
    uri: "rest:post:/hello"
    steps:
    - to:
        uri: "jslt:classpath:expression.jslt"
        parameters:
          contentCache: true
    - to:
        uri: "log:info"
component jslt

JSONata

  • Description: the JSONata component allows processing and transforming JSON data using the JSONata query language. It enables complex operations such as filtering, transforming, and aggregating data.

  • Example

    • The flow starts with a POST request at the endpoint /jsonata.

    • Then, the content of the request is transformed using a JSONata expression stored in the file expression.jsonata.

    • The parameters indicate that:

      • the results of the transformation can be cached (contentCache: true);

      • the input type for the transformation is a JSON string (inputType: JsonString);

      • the output type will also be a JSON string (outputType: JsonString).

    • After that, the data is sent to an external endpoint via a POST request.

Script

Diagram

- from:
      uri: rest:post:/jsonata
      steps:
        - to:
            uri: jsonata:classpath:expression.jsonata
            parameters:
              contentCache: true
              inputType: JsonString
              outputType: JsonString
        - to:
            uri: https://enzcomwvg2nng.x.pipedream.net
            parameters:
              bridgeEndpoint: true
              httpMethod: POST
component jsonata

JSON Schema Validator

  • Description: the JSON Schema Validator component performs validation of the message body.

  • Example

    • The flow starts when a POST request is received at the endpoint /hello.

    • Then, the JSON validation component is used to check if the request body conforms to the schema defined in the file my-json-schema.json.

Script

Diagram

- from:
    uri: "rest:post:/hello"
    steps:
       - to:
           uri: "json-validator:file:/etc/camel/resources/sensedia-integration-camel-poc-configmap/my-json-schema.json"
component json schema validator

XJ

  • Description: the XJ component allows converting XML and JSON documents without the need for intermediate Java objects.

  • Example:

    • The flow starts by receiving a POST request at the /example-xj endpoint.

    • It makes a GET call via HTTPS.

    • It transforms the received XML response into JSON using the XSLT component.

      • The parameter transformDirection: XML2JSON indicates the direction of the transformation, which in this case is from XML to JSON.

    • It converts the JSON into a Java object.

    • Finally, it sets the Content-Type header to application/json to indicate the response type.

Script

Diagram

- from:
    uri: "rest:post:/example-xj"
    steps:
    - toD:
        uri: "https://httpbin.org/xml"
        parameters:
          bridgeEndpoint: true
          httpMethod: GET

    - to:
        uri: "xj:classpath:response_to_json.xslt"
        parameters:
          transformDirection: XML2JSON

    - unmarshal:
        json: {}

    - set-header:
        name: "Content-Type"
        constant: "application/json"
component xj

XSLT

  • Description: the XSLT component is used to transform XML documents using an XSLT stylesheet, allowing data to be converted into different formats or structures.

  • Example

    • The flow starts with a POST request at the endpoint /source-xslt.

    • The EIP toD indicates that the flow should direct execution to a dynamic URI.

    • The route then makes a GET request, obtaining an XML response.

    • It then transforms the XML response into JSON using an XSLT file: response_to_json.xslt.

Script

Diagram

- from:
    uri: "rest:post:/source-xslt"
    steps:
      - toD:
          uri: "https://httpbin.org/xml"
          parameters:
            bridgeEndpoint: true
            httpMethod: GET
      - to:
          uri: "xslt:classpath:response_to_json.xslt"
component xslt
Thanks for your feedback!
EDIT

Share your suggestions with us!
Click here and then [+ Submit idea]