KB Article #181327

JSON to XML filter fails with Staxon mapping convention '@' for XML attributes

Problem

When converting JSON to XML and using the Staxon mapping convention of adding "@" to properties that need to be converted to XML attributes like this:


{
    "Items": {
        "type": "book",
        "@quantity": "1"
    }
}

You get an error like this from the JSON to XML filter:


DEBUG 01/01/20, 00:00:00.000 JSON object names have been updated
DATA 01/01/20, 00:00:00.000 after JSON object name conversion:{[...]"@quantity":"1",[...]}
ERROR 01/01/20, 00:00:00.000 java exception:
java.lang.RuntimeException: Cannot determine next state
at de.odysseus.staxon.event.SimpleXMLEventReader.hasNext(SimpleXMLEventReader.java:128)
at com.sun.xml.internal.stream.writers.XMLEventWriterImpl.add(XMLEventWriterImpl.java:69)
at com.vordel.circuit.json.JSONToXMLProcessor.invoke(JSONToXMLProcessor.java:95)
at com.vordel.circuit.InvocationEngine.invokeFilter(InvocationEngine.java:149)
[...]
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,317]
Message: Cannot read attribute: element has children or text
at de.odysseus.staxon.base.AbstractXMLStreamReader.readAttr(AbstractXMLStreamReader.java:288)
at de.odysseus.staxon.json.JsonXMLStreamReader.readAttrNsDecl(JsonXMLStreamReader.java:123)
at de.odysseus.staxon.json.JsonXMLStreamReader.consumeName(JsonXMLStreamReader.java:145)
at de.odysseus.staxon.json.JsonXMLStreamReader.consume(JsonXMLStreamReader.java:170)
at de.odysseus.staxon.base.AbstractXMLStreamReader.hasNext(AbstractXMLStreamReader.java:411)
at de.odysseus.staxon.event.SimpleXMLEventReader.peek(SimpleXMLEventReader.java:215)
at de.odysseus.staxon.event.SimpleXMLEventReader.hasNext(SimpleXMLEventReader.java:126)
... 10 more
DEBUG 01/01/20, 00:00:00.000 } = 2, filter [JSON To XML]



Resolution


This happens when the JSON object properties marked with '@' are not at the start of the object. To convert the JSON to XML, ensure that all properties using that mapping convention occur prior to any other properties within the JSON object. For example, this JSON would convert correctly due to the @quantity property coming before the type property:


{
    "Items": {
        "@quantity": "1",
        "type": "book"
    }
}