9

Camel Route :

<camelContext xmlns="http://camel.apache.org/schema/spring">

   <dataFormats>
    <xmljson id="xmljson" />
   </dataFormats>

    <route id="route1">
        <from uri="file:C:/Users/User1/InputXML"/>
        <to uri="activemq:queue:MyThread1"/>        
    </route>

    <route id="route2">
        <from uri="activemq:queue:MyThread1"/>    
        <marshal ref="xmljson"/> 
        <bean ref="com.test.OutputProcessor"/>
    </route> 
</camelContext>

Input XML :

<?xml version="1.0" encoding="UTF-8"?>
<Message>
  <to> Tove</to>
 <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</Message>

Actual output :

{"to":" Tove","from":"Jani","heading":"Reminder","body":"Don't forget me this weekend!"}

I want to customize this output. i want to add some mote attributes to the converted json. For Example i want the output json as

  {
    "inputs":[  
                { 
            "inputname":"to",
            "inputValue":"Tove"
            },
            { 
            "inputname":"from",
            "inputValue":"jani"
            },
            { 
            "inputname":"heading",
            "inputValue":"Reminder"
            },
            { 
            "inputname":"body",
            "inputValue":"Don't forget me this weekend!"
            }
        ]
    }

How this can be achieved ?

3
  • Look at content en-richer and message translator EIP in Apache Camel. Commented Jul 27, 2015 at 4:42
  • Are you essentially asking how to convert multiple strings into a single chunk of JSON (held in a variable) in the format you showed, each consisting of a group 4 pairs of data? Or could there be more more than 4 pairs of data, eg CC value? Commented Jul 29, 2015 at 13:55
  • There could be more pairs. What i actually want is to add custom attributes , like "inputname" or"inputtype" in JSon , which are not a part of XML. Commented Jul 29, 2015 at 15:09

3 Answers 3

1

I think an AggregationStrategy might help:

1) Fist you add the aggregationStrategy to your route:

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:start"/>
    <enrich strategyRef="aggregationStrategy">
      <constant>direct:resource</constant>
    <to uri="direct:result"/>
  </route>
  <route>
    <from uri="direct:resource"/>
    ...
  </route>
</camelContext>

<bean id="aggregationStrategy" class="com.ExampleAggregationStrategy" />

2) Then create the class that will get the Body of the message and transform it the way you want, and set the body to the Exchange again. OBS: Here You will need to use a xml API to add the attributes you want to add.

public class ExampleAggregationStrategy implements AggregationStrategy {

    public Exchange aggregate(Exchange original, Exchange resource) {
        Object originalBody = original.getIn().getBody();
        Object resourceResponse = resource.getIn().getBody();
        Object mergeResult = ... // combine original body and resource response
        if (original.getPattern().isOutCapable()) {
            original.getOut().setBody(mergeResult);
        } else {
            original.getIn().setBody(mergeResult);
        }
        return original;
    }

}

More here.

Sign up to request clarification or add additional context in comments.

Comments

0

Is there anything preventing you from using an XSLT component? You can apply that to bring the input XML to a format that directly maps to your desired output JSON format and then push it to xmljson e.g. - (need some clean up to avoid some blank elements)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="Message">
        <inputs>
        <xsl:for-each select="*">
        <inputname><xsl:value-of select="name()" /> </inputname>
        <inputvalue><xsl:value-of select="." /></inputvalue>
        </xsl:for-each>
        </inputs>
    </xsl:template>
</xsl:stylesheet>

2 Comments

Nothing preventing.. XSLT is working like gem. i have been using xsl from last few months . Now i want to use this camel feature .
To best of my knowledge, xmljson component will not give you much control to transform / change the kind of JSON objects are generated. You need to feed it XML that is close to JSON you want - for that you should use XSLT component of Camel.
0

Use the Jackson library. You can programmatically change the output format. Unmarshal is only good for direct mapping and not enrichment. Essentially Unmarshal to xml, add a processor and then create your output Json format.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.