Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response object switching from array to object value based on number of results #91

Open
rmzg opened this issue Dec 1, 2017 · 6 comments

Comments

@rmzg
Copy link

rmzg commented Dec 1, 2017

A problem I frequently run into is when the value of a given key somewhat randomly switches between an array value and an object value. In my most recent they key is the ItemLookupResponse.Items.Item which sometimes contains an array and sometimes contains a single object which tends to cause errors like TypeError: Cannot read property 'length' of undefined.

Obviously the reason why it switches is because sometimes there's multiple values and sometimes there's a single value, which is why you can't use a generic function to translate XML in to JSON since they're not the same type of structure.

This can be worked around in the requiring code with some annoyance but would make for much simpler code if it was fixed in the library code, which in this case means that Items.Item should always be an array.

@dmcquay
Copy link
Owner

dmcquay commented Dec 1, 2017 via email

@rmzg
Copy link
Author

rmzg commented Dec 17, 2017

Obviously it's the xml2js library doing it since you can't actually convert XML to JSON without throwing away a lot of the XML data. This is one of the most basic reasons to not use a generic converter.

@jaribu
Copy link

jaribu commented Jan 18, 2018

It's not due to this option?
`const xml2js = require('xml2js')

const defaultXml2JsOptions = {
explicitArray: false
}`

@jaribu
Copy link

jaribu commented Feb 19, 2018

How can I change this just for test ?

@rmzg
Copy link
Author

rmzg commented Feb 19, 2018

Change what?

@jaribu
Copy link

jaribu commented Feb 20, 2018

explicitArray: false to true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants