I am using ClusterPoint database and I have imported XML document which contains tags and attributes:
<db>
<document>
<id>1</id>
<name lang="en">John</name>
<name lang="ru">Джон</name>
</document>
<document>
<id>2</id>
<name lang="en">Bill</name>
<name lang="ru">Билл</name>
</document>
</db>
When I use the JSON RestAPI to retrieve my document I get the following:
"documents": [
{
"id": "1",
"name": [
"John",
"Джон"
]
},
{
"id": "2",
"name": [
"Bill",
"Билл"
]
}]
Is it possible to somehow get the attribute?
I stumbled on the same problem some time ago too. I would not be surprised if there is no way to get attributes back in JSON (as of now), because documentation does not list such scenario.
It's not just Clusterpoint with similar problems. For example - if you would take particular XML, load it in PHP with simplexml and then encode it to JSON, attributes would be dropped too. XML does not map to JSON 1:1 and things which are ambiguous require special care or they get dropped. There is not enough information on how to convert
And there is no 1 common standard on how to do things. It's clear that this should result in array. But how to structure it? We could convert each element to object and then store objects in array. However what key to give to values? Something like this might be plausible:
But again we run into problem, that its hard to convert data back as we do not know if this came from attributes. We might use different setup:
Then @ would indicate that this came from attribute. Or like this:
to indicate that #value always holds value which was in tag. To sum up - it looks like currently there is no way to get attributes in JSON. If there is - guys/girls from Clusterpoint should add detailed info to their docs. However if there really is no way to get attributes, then I bet that in future versions there will be some documented standard that Clusterpoint will use.
While there is no documented standard on how Clusterpoint handles XML attribute conversion to JSON and you really need attributes - just use XML. Or if you want JSON you can reshape your documents to something that's does not require attributes.