node-apac icon indicating copy to clipboard operation
node-apac copied to clipboard

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

Open rmzg opened this issue 7 years ago • 6 comments

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.

rmzg avatar Dec 01 '17 04:12 rmzg

That sounds very logical. I'm curious to check if the response from Amazon is causing this or if it is the XML to JSON that is causing this issue. It sounds like you are saying XML to JSON is the culprit. In any case, normalizing it sounds like a great idea. I probably won't get around to this soon. Feel free to submit a PR if you want it done soon.

On Thu, Nov 30, 2017 at 9:57 PM rmzg [email protected] wrote:

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.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/dmcquay/node-apac/issues/91, or mute the thread https://github.com/notifications/unsubscribe-auth/AAHIcS34oTn-SJAHji40e-R_V22Dx44Tks5s74c3gaJpZM4Qx3lM .

dmcquay avatar Dec 01 '17 07:12 dmcquay

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.

rmzg avatar Dec 17 '17 00:12 rmzg

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

const defaultXml2JsOptions = { explicitArray: false }`

jaribu avatar Jan 18 '18 13:01 jaribu

How can I change this just for test ?

jaribu avatar Feb 19 '18 14:02 jaribu

Change what?

rmzg avatar Feb 19 '18 22:02 rmzg

explicitArray: false to true

jaribu avatar Feb 20 '18 12:02 jaribu