I am receiving xml data from an api, check this link https://developers.cj.com/docs/rest-apis/advertiser-lookup
I need to parse xml to json or js object in order to use it properly.
I've tried xml2js, it converts stuff in the following way.
<?xml version="1.0" encoding="UTF-8"?>
<cj-api><error-message> You must provide an Authorization header.</error-message></cj-api>
{
'cj-api': { 'error-message': [ 'You must provide an Authorization header.' ] }
}
In this case I dont need the array, just the simple value. Is there a working parser that I can use?
you can use camaro like this
const { transform } = require('camaro')
async function main() {
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<cj-api><error-message> You must provide an Authorization header.</error-message></cj-api>
`
const template = {
errorMessage: '/cj-api/error-message'
}
const output = await transform(xml, template)
console.log(output.message);
}
main()
output
You must provide an Authorization header.