JSON output? #4

Closed
opened 2012-07-05 17:49:50 +01:00 by Wred · 3 comments
Wred commented 2012-07-05 17:49:50 +01:00 (Migrated from github.com)

Hi Andy,
Quick question: How can I set the serialization options so that I get JSON instead of xml in the return?

http://docs.basex.org/wiki/Serialization

Thanks!

Hi Andy, Quick question: How can I set the serialization options so that I get JSON instead of xml in the return? http://docs.basex.org/wiki/Serialization Thanks!
apb2006 commented 2012-07-06 13:46:46 +01:00 (Migrated from github.com)

Currently basex-node has no special handling for JSON. It would be a useful addition.

However..
If you set a json type serialization using one of the provided methods "json" or "jsonml" http://docs.basex.org/wiki/Serialization

  • "jsonml" is good for arbitrary XML
  • "json" requires your xml result is in a particular format, but allows you to create JSON tailored for a particular target API.

The basex-node reply.result, which is always a string, can be parsed via JSON.parse
Here is an example json.js file created in the examples folder

/*
 * This example shows how JSON can be returned.
 */
var basex  = require("../index");
var client = new basex.Session();
function print(err, reply) {
        if (err) {
                console.log("Error: " + err);
        } else {
                console.log(reply);
                var json=JSON.parse(reply.result);
                console.log("JSON:",json);
        }
};
var xq="<foo a='45'><goo>some text</goo></foo>";
client.execute("xquery declare option output:method 'jsonml';"+xq,print);
client.close();

Result from node json.js

 { result: '["foo", {"a":"45"},\n  ["goo",\n    "some text"]]',
  info: '\nQuery executed in 0.36 ms.\n',
  ok: true }
JSON: [ 'foo', { a: '45' }, [ 'goo', 'some text' ] ]
Currently basex-node has no special handling for JSON. It would be a useful addition. However.. If you set a json type serialization using one of the provided methods "json" or "jsonml" http://docs.basex.org/wiki/Serialization - "jsonml" is good for arbitrary XML - "json" requires your xml result is in a particular format, but allows you to create JSON tailored for a particular target API. The basex-node `reply.result`, which is always a string, can be parsed via `JSON.parse` Here is an example `json.js` file created in the examples folder ``` javascript /* * This example shows how JSON can be returned. */ var basex = require("../index"); var client = new basex.Session(); function print(err, reply) { if (err) { console.log("Error: " + err); } else { console.log(reply); var json=JSON.parse(reply.result); console.log("JSON:",json); } }; var xq="<foo a='45'><goo>some text</goo></foo>"; client.execute("xquery declare option output:method 'jsonml';"+xq,print); client.close(); ``` Result from `node json.js` ``` javascript { result: '["foo", {"a":"45"},\n ["goo",\n "some text"]]', info: '\nQuery executed in 0.36 ms.\n', ok: true } JSON: [ 'foo', { a: '45' }, [ 'goo', 'some text' ] ] ```
apb2006 commented 2012-07-06 13:57:46 +01:00 (Migrated from github.com)

Using SERIALIZER

/*
 * This example shows how JSON can be returned.
 */
var basex  = require("../index");
var client = new basex.Session();
function print(err, reply) {
        if (err) {
                console.log("Error: " + err);
        } else {
                console.log(reply);
                var json=JSON.parse(reply.result);
                console.log("JSON:",json);
        }
};
var xq="<foo a='45'><goo>some text</goo></foo>";
client.execute("SET SERIALIZER method=jsonml",basex.print);
client.execute("XQUERY "+xq,print);
client.close();
Using SERIALIZER ``` javascript /* * This example shows how JSON can be returned. */ var basex = require("../index"); var client = new basex.Session(); function print(err, reply) { if (err) { console.log("Error: " + err); } else { console.log(reply); var json=JSON.parse(reply.result); console.log("JSON:",json); } }; var xq="<foo a='45'><goo>some text</goo></foo>"; client.execute("SET SERIALIZER method=jsonml",basex.print); client.execute("XQUERY "+xq,print); client.close(); ```
Wred commented 2012-07-06 17:51:10 +01:00 (Migrated from github.com)

Ah great. Thanks!

Ah great. Thanks!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
quodatum/basex-node#4
No description provided.