Hello,
I'm new in the XML databases world, evaluating BaseX and eXist-db for a new project.
BaseX seems really clean and lightweight.
I would like to use it via the REST API and with a json output.
I test with the factbook.xml example database and the method parameter set to "json" : http://localhost:8984/rest/factbook?query=//city/name&wrap=yes works fine but http://localhost:8984/rest/factbook?query=//city/name&wrap=yes&metho... returns [BXJS0002] JSON serializer: <json> expected as root node.
I don't understand why. I need json serialization whatever is the XML returned by the query.
And I don't understand how to use the json options via the REST API. http://docs.basex.org/wiki/JSON_Module An example could be fine to help.
Best regards
Florent
Hello Florent,
there are actually is an example given (Example 4) in the doc page you mention, although it is just in the other direction (i.e. from json to xml). But as the JSON serialization is lossless the same rules apply in the other direction as well.
So if you want to serialize to JSON, you will have to need a <json/> element as root, e.g.
<json objects="json"> <myvalue>test</myvalue> </json>
You might be wondering why it is necessary to have such a format and the XML is not simply automatically converted to JSON. This is directed to the different nature of JSON and XML; whereas JSON has different types such as numbers and strings, XML just uses Strings. So the conversion can not automatically detect what value should be a string or a number.
If you are currently evaluating let me add that you might want to take a look at RestXQ (see https://docs.basex.org/wiki/RESTXQ for more details). This way you could also write one simply Rest interface which automatically wraps your data into a <json/> element (either very simply because you have just strings or by certain rules)) and delivers JSON back do you. Something like this (untested!):
declare %rest:path("") %rest:query-param("q", "{$q}") %output:method("json") function page:json($q as xs:string) { <json objects="json">{xquery:eval($q)}</json> };
Of course you will have a much cleaner architecture if you define certain REST endpoints for your application and not simply evaluate every incoming query (which also introduces certain security problems, so don't run this in production!).
Hope you stay with BaseX :-)
Cheers, Dirk
On 07/04/14 17:41, Florent Gallaire wrote:
Hello,
I'm new in the XML databases world, evaluating BaseX and eXist-db for a new project.
BaseX seems really clean and lightweight.
I would like to use it via the REST API and with a json output.
I test with the factbook.xml example database and the method parameter set to "json" : http://localhost:8984/rest/factbook?query=//city/name&wrap=yes works fine but http://localhost:8984/rest/factbook?query=//city/name&wrap=yes&metho... returns [BXJS0002] JSON serializer: <json> expected as root node.
I don't understand why. I need json serialization whatever is the XML returned by the query.
And I don't understand how to use the json options via the REST API. http://docs.basex.org/wiki/JSON_Module An example could be fine to help.
Best regards
Florent
basex-talk@mailman.uni-konstanz.de