Converts an XML message body to JSON using a structural, heuristic mapping: elements and attributes both become JSON properties, and a repeated element becomes a JSON array while the same element occurring once becomes an object.
Element order, mixed content, and namespaces are not preserved. Only messages with an XML content type are converted; other bodies pass through unchanged.
Malformed XML aborts the exchange with a Problem Details response (400 in the request flow, 500 in the response flow).
The character encoding is taken from the Content-Type header if present, otherwise read from the XML prolog, and the result is always UTF-8 encoded JSON.
Given an XML Schema, the mapping follows the schema instead of the heuristics, so the JSON has a fixed shape: values are typed as declared, an element that may repeat is always an array, and a document the schema does not declare is rejected. See tutorials/xml/20-XML-to-JSON.yaml and tutorials/xml/25-XML-to-JSON-with-XSD.yaml.
xml2Json:
[ keepString: true | false ] # default: false
[ convertNilAttributeToNull: true | false ] # default: true
forceList: # 0..*; always render these element names as arrays
- <element-name>
...
xml2Json:
xmlSchema: <url or path> # replaces the heuristic settings above
Example Configuration
api:
port: 2000
flow:
- xml2Json: {}
api:
port: 2000
flow:
- xml2Json: {}
Syntax
xml2Json:
convertNilAttributeToNull: <boolean>
forceList:
- <string>
keepString: <boolean>
xmlSchema: <string>
xml2Json:
convertNilAttributeToNull: <boolean>
forceList:
- <string>
keepString: <boolean>
xmlSchema: <string>
Attributes
| Name | Required | Default | Description | Examples |
|---|---|---|---|---|
| convertNilAttributeToNull | false | true | If true, converts elements marked xsi:nil="true" into JSON null values. | false |
| forceList | false | - | - | - |
| keepString | false | false | If true, keeps every element's text as a JSON string instead of coercing numeric- or boolean-looking text into JSON numbers and booleans. | true |
| xmlSchema | false | - | An XML Schema (XSD) that drives the mapping instead of the heuristics: a URL or a file path, relative to the configuration file. Its imports and includes are loaded too. Values are typed as the schema declares them, an element that may repeat is always an array, and xsi:nil becomes null. The result has the root element as its single key, but the structure differs from the heuristic mapping: attributes become @-prefixed keys, the text of an element with attributes is put under $value, and keys are local names without a prefix. A document whose root element the schema does not declare globally is rejected with 400 in the request flow and 500 in the response flow. Cannot be combined with keepString, convertNilAttributeToNull or forceList. | books.xsd |