Converts XML to JSON and vice-versa using node-expat parser
Overall
score
93%
Convert XML input into JSON-friendly objects while forcing arrays for repeated tags either globally or for specific tag names. Shapes should stay consistent even when a tag appears only once.
<fleet><driver>Ada</driver><vehicle><id>V1</id></vehicle><vehicle><id>V2</id></vehicle></fleet> forces both driver and vehicle into arrays, keeping vehicle IDs V1 then V2 in order. @test<garage><vehicle><id>R1</id></vehicle><driver>Ada</driver></garage> with tag names ["vehicle"] returns vehicle as a one-item array while driver stays as a simple string. @test<inventory><item>alpha</item><item>beta</item><note>single</note></inventory> with tag names ["item"] yields item as ["alpha", "beta"] in XML order and leaves note as a plain string. @test@generates
/**
* Parses XML into an object while forcing every element name to be represented as an array.
* @param {string|Buffer} xmlInput
* @returns {Record<string, any>}
*/
export function parseAllArrays(xmlInput);
/**
* Parses XML into an object while forcing only selected element names into arrays.
* @param {string|Buffer} xmlInput
* @param {string[]} arrayTagNames
* @returns {Record<string, any>}
*/
export function parseSelectiveArrays(xmlInput, arrayTagNames);Transforms XML into JSON objects with configurable array notation.
Install with Tessl CLI
npx tessl i tessl/npm-xml2jsondocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10