RestXQ output serialization

Wednesday, 4 November 2026 · 4 messages
ML
Marco Lettere4 Nov 2026 11:40 #

Hi!

According to the documentation [1] I was expecting both of these RestXQ to return a json string representation.

They return the XML representation instead.

Any help on what I'm understaing wrongly?

Thanks,

Marco.

[1]

https://docs.basex.org/main/RESTXQ#custom_response

[2]

declare
   %rest:path("/test/json")
   %rest:GET
function page:test-json() {
   (
     <rest:response>
       <http:response status="200">
         <http:header name="content-type" value="application/json"/>
       </http:response>
     </rest:response>,
     <json type="object"><x type="number">1</x></json>
   )
};

declare
   %rest:path("/test/json2")
   %rest:GET
function page:test-json2() {
   (
     web:response-header({ "media-type" : "application/json"}),
     <json type="object"><x type="number">1</x></json>
   )
};
CG
Christian Grün4 Nov 2026 12:51 #

Hi Marco,

The Content-Type header and the media-type parameter only set the label that goes into the HTTP header. They don’t change how the result is serialized. The serialization method is still XML, so your <json>​ element is sent as XML. To get JSON output, you need to set the serialization method to json. There are three ways to do this:

(: 1. annotation :)
declare
%rest:path('/test/json')
%rest:GET
%output:method('json')
function page:test-json() {
<json type='object'><x type='number'>1</x></json>
};

(: 2. rest:response :)
declare
%rest:path('/test/json2')
%rest:GET
function page:test-json2() {
<rest:response>
<output:serialization-parameters>
<output:method value='json'/>
</output:serialization-parameters>
</rest:response>,
<json type='object'><x type='number'>1</x></json>
};

(: 3. web:response-header :)
declare
%rest:path('/test/json3')
%rest:GET
function page:test-json3() {
web:response-header({ 'method': 'json' }),
<json type='object'><x type='number'>1</x></json>
};

With method=json, the content type is set to application/json automatically. You could also skip the XML representation and return a map such as { 'x': 1 } directly.

Hope this helps, Christian

Quoted text · Marco Lettere · 42 lines
Marco Lettere via BaseX-Talk <basex-talk at mailman.uni-konstanz.de> schrieb am Mittwoch, 4. November 2026 um 11:41: > Hi! > > According to the documentation [1] I was expecting both of these RestXQ to return a json string representation. > > They return the XML representation instead. > > Any help on what I'm understaing wrongly? > > Thanks, > > Marco. > > [1] > > https://docs.basex.org/main/RESTXQ#custom_response > > [2] > > declare > %rest:path("/test/json") > %rest:GET > function page:test-json() { > ( > <rest:response> > <http:response status="200"> > <http:header name="content-type" value="application/json"/> > </http:response> > </rest:response>, > <json type="object"><x type="number">1</x></json> > ) > }; > > declare > %rest:path("/test/json2") > %rest:GET > function page:test-json2() { > ( > web:response-header({ "media-type" : "application/json"}), > <json type="object"><x type="number">1</x></json> > ) > };
ML
Marco Lettere4 Nov 2026 13:19 #

Ok, I see. Thanks.

But then the following sentence in https://docs.basex.org/main/RESTXQ#custom_response sounds sort of misleading to me:

"A Content-Type header is sent unchanged: no charset is added, and its value is also used as media-type for serializing the response body."

M.

Quoted text · Christian Grün · 95 lines
On 11/4/26 12:51, Christian Grün wrote: > Hi Marco, > > The Content-Type header and the media-type parameter only set the > label that goes into the HTTP header. They don’t change how the result > is serialized. The serialization method is still XML, so your > |<json>|​ element is sent as XML. To get JSON output, you need to set > the serialization method to json. There are three ways to do this: > > (: 1. annotation :) > declare > %rest:path('/test/json') > %rest:GET > %output:method('json') > function page:test-json() { > <json type='object'><x type='number'>1</x></json> > }; > > (: 2. rest:response :) > declare > %rest:path('/test/json2') > %rest:GET > function page:test-json2() { > <rest:response> > <output:serialization-parameters> > <output:method value='json'/> > </output:serialization-parameters> > </rest:response>, > <json type='object'><x type='number'>1</x></json> > }; > > (: 3. web:response-header :) > declare > %rest:path('/test/json3') > %rest:GET > function page:test-json3() { > web:response-header({ 'method': 'json' }), > <json type='object'><x type='number'>1</x></json> > }; > > With method=json, the content type is set to application/json > automatically. You could also skip the XML representation and return a > map such as `{ 'x': 1 }` directly. > > Hope this helps, > Christian > > > Marco Lettere via BaseX-Talk <basex-talk at mailman.uni-konstanz.de> > schrieb am Mittwoch, 4. November 2026 um 11:41: >> >> Hi! >> >> According to the documentation [1] I was expecting both of these >> RestXQ to return a json string representation. >> >> They return the XML representation instead. >> >> Any help on what I'm understaing wrongly? >> >> Thanks, >> >> Marco. >> >> [1] >> >> https://docs.basex.org/main/RESTXQ#custom_response >> >> [2] >> >> declare >> %rest:path("/test/json") >> %rest:GET >> function page:test-json() { >> ( >> <rest:response> >> <http:response status="200"> >> <http:header name="content-type" value="application/json"/> >> </http:response> >> </rest:response>, >> <json type="object"><x type="number">1</x></json> >> ) >> }; >> >> declare >> %rest:path("/test/json2") >> %rest:GET >> function page:test-json2() { >> ( >> web:response-header({ "media-type" : "application/json"}), >> <json type="object"><x type="number">1</x></json> >> ) >> }; >> >
CG
Christian Grün4 Nov 2026 13:42 #

I agree, the sentence was easy to misread. I have clarified the documentation.


⚡Generated with XQuery