JSON serialisation for results
Tuesday, 3 September 2013 · 2 messages
BS
Bell, Darren S3 Sep 2013 18:18 #
Hi, I'm new to BaseX. We are running a simple XQuery:
declare variable $documentguid as xs:string external; <utterances> { for $targetelements in /*:TEI/*:text/*:body/*:u[/*:TEI/*:teiHeader/*:fileDesc/*:publicationStmt/*:idno[@type='qudexdocument']=$documentguid]
return
<utterance>
<guid>{data($targetelements/@xml:id)}</guid>
<speaker>{data($targetelements/@who)}</speaker>
<text>{data($targetelements/text())}</text>
</utterance>
}
</utterances>by calling http://dahdsst2:8984/rest/DF?run=interviewutterances.xq&documentguid=q-25d5254a-efd5-4765-b3a0-19162e141203&method=jsonml
All works well but our UI developers are not happy with the JSONML as a serialization format. I changed the output method to 'json' and this returns "[BXJS0002] JSON serialization: <json> expected as root node." So then I changed the <utterances> root element to <json> and then I get: [BXJS0002] JSON serialization: Element <json> must be typed as "object" or "array".
I'm a bit lost at this point. I've consulted the documentation but there is not very much detail about the specific steps to go about serialising an XQuery output as JSON.
The standard JSON format I'm trying to output is like so:
{
"utterances": {
"utterance": [
{
"guid": "q-2f0de4c5-7dfb-e211-b501-000bdb5cc6d5",
"speaker": "interviewer",
"text": "I'd like to start, if I may, by asking you your birth date."
},
{
"guid": "q-300de4c5-7dfb-e211-b501-000bdb5cc6d5",
"speaker": "subject",
"text": "November 9th, 1902."
},
{
"guid": "q-310de4c5-7dfb-e211-b501-000bdb5cc6d5",
"speaker": "interviewer",
"text": "Could you tell me how many children there were in your family?"
}
]
}
}Any tips on how to go about this in BaseX would be much appreciated, Regards,
___________________________________
DARREN BELL
Data and Services Developer
___________________________________
T +44(0) 1226 272144
E dbell at essex.ac.uk
W ukdataservice.ac.uk
___________________________________
UK Data Service
UK Data Archive
University of Essex
___________________________________
Legal Disclaimer: Any views expressed by the sender of this message are not necessarily those of the UK Data Service or the UK Data Archive. This email and any files with it are confidential and intended solely for the use of the individual(s) or entity to whom they are addressed.
DK
Dirk Kirsten3 Sep 2013 18:31 #
Hello Darren,
did you take a look at the documentation at
https://docs.basex.org/wiki/JSON#Examples, especially Example 4 might be helpful. The conversation rules are described at
https://docs.basex.org/wiki/JSON#JSON_Functions
So basically you will have to define what type a node will be in the resulting JSON. Something like the following should work (I did not test this):
<json objects="json utterances" arrays="uterrance">
<utterances> { for $targetelements in
/*:TEI/*:text/*:body/*:u[/*:TEI/*:teiHeader/*:fileDesc/*:publicationStmt/*:idno[@type='qudexdocument']=$documentguid]
return
<utterance>
<guid>{data($targetelements/@xml:id)}</guid>
<speaker>{data($targetelements/@who)}</speaker>
<text>{data($targetelements/text())}</text>
</utterance>
}
</utterances>
</json>Cheers,
Dirk
Quoted text · Darren S · 70 lines
On 09/03/2013 06:18 PM, Bell, Darren S wrote: > Hi, I'm new to BaseX. We are running a simple XQuery: > > declare variable $documentguid as xs:string external; <utterances> { for $targetelements in /*:TEI/*:text/*:body/*:u[/*:TEI/*:teiHeader/*:fileDesc/*:publicationStmt/*:idno[@type='qudexdocument']=$documentguid] > return > <utterance> > <guid>{data($targetelements/@xml:id)}</guid> > <speaker>{data($targetelements/@who)}</speaker> > <text>{data($targetelements/text())}</text> > </utterance> > } > </utterances> > > by calling http://dahdsst2:8984/rest/DF?run=interviewutterances.xq&documentguid=q-25d5254a-efd5-4765-b3a0-19162e141203&method=jsonml > > > All works well but our UI developers are not happy with the JSONML as a serialization format. > I changed the output method to 'json' and this returns "[BXJS0002] JSON serialization: <json> expected as root node." > So then I changed the <utterances> root element to <json> and then I get: [BXJS0002] JSON serialization: Element <json> must be typed as "object" or "array". > > I'm a bit lost at this point. I've consulted the documentation but there is not very much detail about the specific steps to go about serialising an XQuery output as JSON. > > > The standard JSON format I'm trying to output is like so: > { > "utterances": { > "utterance": [ > { > "guid": "q-2f0de4c5-7dfb-e211-b501-000bdb5cc6d5", > "speaker": "interviewer", > "text": "I'd like to start, if I may, by asking you your birth date." > }, > { > "guid": "q-300de4c5-7dfb-e211-b501-000bdb5cc6d5", > "speaker": "subject", > "text": "November 9th, 1902." > }, > { > "guid": "q-310de4c5-7dfb-e211-b501-000bdb5cc6d5", > "speaker": "interviewer", > "text": "Could you tell me how many children there were in your family?" > } > ] > } > } > > Any tips on how to go about this in BaseX would be much appreciated, Regards, > > > ___________________________________ > DARREN BELL > Data and Services Developer > ___________________________________ > T +44(0) 1226 272144 > E dbell at essex.ac.uk > W ukdataservice.ac.uk > ___________________________________ > UK Data Service > UK Data Archive > University of Essex > ___________________________________ > Legal Disclaimer: Any views expressed by the sender of this message are not necessarily those of the UK Data Service or the UK Data Archive. > This email and any files with it are confidential and intended solely for the use of the individual(s) or entity to whom they are addressed. > > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >
--
Dirk Kirsten, BaseX GmbH, http://basex.org
|-- Firmensitz: Blarerstrasse 56, 78462 Konstanz
|-- Registergericht Freiburg, HRB: 708285, Geschäftsführer:
| Dr. Christian Grün, Dr. Alexander Holupirek, Michael Seiferle `-- Phone: 0049 7531 28 28 676, Fax: 0049 7531 20 05 22
Written for BaseX 7.7. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery