Skip to content

Conventions

A35G edited this page Sep 10, 2026 · 1 revision

Conventions

These four rules are the core of the library: once you understand them, you already know how to use it well. They apply in both directions (JSON → XML and XML → JSON).

Simple elements

{ "name": "Mario" }
<name>Mario</name>

Attributes → @ prefix

{
  "request": {
    "@code": "",
    "@typeReq": "ABC",
    "customer": "Mario Rossi"
  }
}
<request code="" typeReq="ABC">
  <customer>Mario Rossi</customer>
</request>

Direct text → #text key

Used when a node has both attributes and text (so-called mixed content):

{ "areaCode": { "@codeArea": "XXX", "#text": "contains <tag>" } }
<areaCode codeArea="XXX"><![CDATA[contains <tag>]]></areaCode>

Lists → repetition without a wrapper

A JSON array becomes a series of nodes with the same name, one per element, without an intermediate wrapper tag:

{ "Notes": { "Note": [ {"Main": "0"}, {"Main": "1"} ] } }
<Notes>
  <Note><Main>0</Main></Note>
  <Note><Main>1</Main></Note>
</Notes>

CDATA is automatic, not manual

There is no key to "force" CDATA: the library decides on its own based on the content. A value only ends up in CDATA if it contains one of the characters < > & ' " or a newline.

{ "description": "Text with special <tag>" }
<description><![CDATA[Text with special <tag>]]></description>
{ "vatNumber": "12345678901" }
<vatNumber>12345678901</vatNumber>

If you never want CDATA, pass useCdata: false to the JsonToXmlConverter constructor.


⬅️ Previous page: Basic-Usage · ➡️ Next page: Round-Trip-and-Limitations

Clone this wiki locally