User JSON output in Apache Camel xmljson

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> 

XML input:

 <?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 configure this output. I want to add some mote attributes to converted json. For example, I want json output as

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

How can this be achieved?

+8
json xml xml-parsing apache-camel
source share
3 answers

I think AggregationStrategy might help:

1) Fist you add a Straightgy aggregation 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 a class that will receive the Message Body and convert it the way you want, and set the body back to Exchange. OBS: here you need to use the 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 details here .

0
source share

Is there anything hindering the use of the XSLT component? You can apply this to bring the input XML into a format that directly maps to your desired JSON output format, and then click on xmljson for example. - (need to be cleaned a bit to avoid empty items)

 <?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> 
0
source share

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

0
source share

All Articles