I currently have the following simplified JSON (test.json):
{"type":"monkey","food":"banana"},{"type":"dog","food":"bone"},{"type":"cat","food":"fish"}
Doing the following:
from("file:/tmp/input").routeId("test")
.unmarshal().json(JsonLibrary.Jackson)
.log("After unmarshall:\n ${body}");
When dropping the JSON file into the input folder, I only get the following map:
{type=monkey, food=banana}
How to get all items into the mapping?
If you want to get all then put them to an array like
[{"type":"monkey","food":"banana"},{"type":"dog","food":"bone"},{"type":"cat","food":"fish"}]
Then setup dataFormat with useList option true. In Xml DSL that i tried is
<dataFormats>
<json id="json" library="Jackson" useList="true"/>
</dataFormats>
In Java is
JacksonDataFormat json = new JacksonDataFormat();
json.useList();
Then use unmarshall with the preceding data format
XML DSL:
<unmarshal ref="json"/>
So you should have :
from("file:/tmp/input").routeId("test")
.unmarshal().json(json)
.log("After unmarshall:\n ${body}");
And the output is a list:
[{type=monkey, food=banana}, {type=dog, food=bone}, {type=cat, food=fish}]
The JSON you're trying to parse is not a valid JSON. If you have control of what's being sent to Camel, try wrapping it in an array, like this:
[{"type":"monkey","food":"banana"},{"type":"dog","food":"bone"},{"type":"cat","food":"fish"}]
This will net you an array of objects and should parse correctly.
If you can't control the input, you can also add the array marks as part of Camel processing, just before the unmarshal() call
Related
I have a json file that i want to sent it's contents to a Kafka consumer. The kafka uses Avro and a Schema that adheres to the Json i want to send .So is there a way to read the json and then send the whole contents of it through kafka without the need to first parse the json and then send everything separately with keys and values?
Thanks.
Assuming you're using the Schema Registry, sure, you can remove whitespace from the file, then read it as stdin
kafka-avro-console-consumer ... < $(cat file.json)
Otherwise, you would need to write your own producer to be able to plugin the Avro serializer
I'm calling a soap webservice from my java application.
I get response and I want to parse it and get data.
The problem is that field <tranData>, contains structure with >< instead of <>. How can I parse this document to get data from field <tranData>?
This is response structure:
<response>
<Portfolio>
<ID>1</ID>
<holder>2</holder>
</Portfolio>
<tranData> <responseOne><header><code>1</code></header></responseOne></tranData>
Please remember that, this is only a example of response, and the amount of data will be much bigger, so the solution should be fast.
What you show us is the actual document as it is received over the wire, right? So <tranData> contains an XML string that has been escaped to not interfere with the markup of the rest of the containing document.
When you read the content of the <tranData> element, the XML processor will 'unescape' the string and give you the 'original' value:
<responseOne><header><code>1</code></header></responseOne>
What you do with that value is a different story. You can parse it as yet another XML document and retrieve the value of the <code> element, or just pass the string along to some other processing step.
I'm trying to parse a web service response message in the following format (message tree):
Message
Properties
Properties..[]
DFDL
ObjectIWantUnmarshalled
AllItsDataIwant[]
And unmarshal the "ObjectIWantUnmarshalled". However, this data is in DFDL format.
In my request, I use the following line in order to format from XML to DFDL:
Document outDocument = outMessage.createDOMDocument(MbDFDL.PARSER_NAME);
But there doesn't seem to be a way to to the opposite, of DFDL to XML.
I have tried:
Document outDocument = inMessage.createDOMDocument(MbXMLNSC.PARSER_NAME);
As well as other attempts to simply unmarshal the data directly from the MbMessage:
jaxbContext_COBOL.createUnmarshaller().unmarshal(inMessage.getDOMDocument())
But I have not been able to get a Document node this way, or any other way, it is always null.
Probably a lot too late, but you were going about this the wrong way.
When using WMB and IIB you should use the built-in XML support - not the javax.XML.* class library. So instead of using the JAXB unmarshaller, you should
create an XMLNSC tree under the output message root
copy the input DFDL message tree to the output XMLNSC message tree ( one line )
...and the message flow will serialize ( unmarshall ) the tree as XML whenever it needs to - when it encounters an output node, or when you call outMessage.toBitstream().
i tried to use Apache Avro on project... and i've met some difficulties
avro serialization/ deserialization work like a charm ... but i get decoder exceptions.. like unknown union branch blah-blah-blah... in case incomming json does't contain namepsace record ...
e.g.
"user":{"demo.avro.User":{"age":1000... //that's ok
"user":{"age":1000... //org.apache.avro.AvroTypeException: Unknown union branch age
I cannot put object in default namespace... but it is important to parse incoming json regardless it contains namespace node or not
Could you help me to fix it
If you use JSON, why are you using Avro decoders? There are tons of JSON libraries which are designed to work with JSON: with Avro, the idea is to Avro's own compact format, and JSON is mostly used for debugging (i.e. you can expose Avro data as JSON if necessary).
I'm trying to convert a JSON string to XML using jsonlib in Java.
JSONObject json = JSONObject.fromObject(jsonString);
XMLSerializer serializer = new XMLSerializer();
String xml = serializer.write( json );
System.out.println(xml);
The error that I get is
nu.xom.IllegalNameException: 0x24 is not a legal NCName character
The problem here is that I have some properties in my JSON that are invalid XML characters. eg. I have a property named "$t". The XMLSerializer throws the exception while trying to create a XML tag in this name because $ is not allowed in XML tag names. Is there any way in which I can override this XML well formedness check done by the serializer?
First I'd suggest to add the language you are using (it is Java, right?).
You could override the method where it checks your XML tag name to do nothing.
I took a look at the spec for the json-lib XMLSerializer and to my surprise it seems to have no option for serialising a JSON object whose keys are not valid XML names. If that's the case then I think you will need to find a different library.
You could loop over json.keySet (recursively if necessary) and replace invalid keys with valid ones (using remove and add).