-
-
Notifications
You must be signed in to change notification settings - Fork 312
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
Missing support for array conversion #181
Comments
😄 Have you checked for the already raised issues and the discussion on the same topic? Please give us an example so we can understand if your rasied concern is different. |
Hi, It is the same as issue #166 Being a conceptual issue when making a conversion from XML to JSON, I think it would be nice if a solution is supported by the library. Thanks |
There is no standard for XML to JSON/JS Object conversion. Hence, we need to finalize the response which suits to our implementation and users expectation. People feel its need but no one yet given us the clear requirement. We don't want hurry. Because many changes cant be reverted once the users at world level start using them. |
I'm closing this issue as it is duplicate for the other issue. If you'll feel we should keep it open, please reopen |
"People feel its need but no one yet given us the clear requirement." In the options we can pass for parsing, add a new optional array. This way there is no conflict with the existing way of working. arrayAccessFormPaths : [] - Array access paths - use this option to configure paths to XML elements always in "array form". You can configure beforehand paths to all your array elements based on XSD or your knowledge. Every path could be a simple string (like 'parent.child1.child2'), a regex (like /.*.child2/), or a custom function. Default is empty |
This is not a clear requirement. I doubt if you have seen the previous discussions. |
Yes, this is a missing feature. |
Use cases: We have a lot of use cases like below. We do not control the XML that is sent to us, we just need to work with it. <report>
<store>
<region>US</region>
<inventory>
<item>
<name>Banana</name>
<count>200</count>
</item>
<item>
<name>Apple</name>
<count>100</count>
</item>
</inventory>
</store>
<store>
<region>EU</region>
<inventory>
<item>
<name>Banana</name>
<count>100</count>
</item>
</inventory>
</store>
</report> which {
"report": {
"store": [
{
"region": "US",
"inventory": {
"item": [
{
"name": "Banana",
"count": "200"
},
{
"name": "Apple",
"count": "100"
}
]
}
},
{
"region": "EU",
"inventory": {
"item": {
"name": "Banana",
"count": "100"
}
}
}
]
}
} Note that the type of Basically this is the reason we can not use |
Thanks for highlighting this @Kaarel . So if let's suppose, we have some option which basically renders a nested tag always an array, it'll look like as follow; {
"report": [
{
"store": [
{
"region": "US",
"inventory": [
{
"item": [
{
"name": "Banana",
"count": "200"
},
{
"name": "Apple",
"count": "100"
}
]
}
]
},
{
"region": "EU",
"inventory": [
{
"item": [
{
"name": "Banana",
"count": "100"
}
]
}
]
}
]
}
]
} will it solve the purpose? |
I have started the discussion here; |
Yes, specifically the strict mode in the examples of #190 |
I'm using openlayers, it expects single values be a collection (array) and not unwrapped (object). It checks the parent node length but fails when the single value is unwrapped. Having a configuration option for this would mean I could use fast-xml-parser. |
@amitguptagwl Yes that's exactly what's needed. I knew it was coming but didn't think it was available on npm yet? |
It is available on Github. I'll publish that on Github tomorrow or on weekend. |
I don't think it is wishfull that every single node is transformed into an array. We should be able to control which node names have to always convert to array. You may have levels in the XML that will only occur once. Take the tag for example in the above XML. A desired solution would be that the user can pass in an array of nodenames which should always be transformed to array. Example:
|
I'm not sure how many users wish to use |
In case a node exists multiple time in the same parent, it will automatically be converted to a JS array.
If the node occurs only once however in the dataset (eventhough it could occur multiple times in theory), the conversion will create an object cause it has no way of knowing it should convert it to an array.
Other libraries have support for this by adding an array in the parsing options containing a list of elements that always need to be converted to an array.
Example https://github.com/abdolence/x2js has support via 'arrayAccessFormPaths'.
I am surprised this basic conceptual issue in xml to json conversion is not yet covered by this widely used library. Makes for a lot of painful checks on the output.
The text was updated successfully, but these errors were encountered: