Options to generate JSON file
Wednesday, 26 January 2011 · 4 messages · last reply 26 Feb 2011
JC
Jan Vlčinský (CAD)26 Jan 2011 20:47 #
Hi
I shall export JSON file based on XML structures I have in my XQuery. Here I found http://demo.exist-db.org/xquery/json-test.xql/source sample, how eXistDB can produce JSON data.
It is like
import module namespace json="http://www.json.org";
04.
05.declare option exist:serialize "method=xhtml media-type=text/html";
let $xml1 :=
08.<root>
09.<h1>H</h1>
10.<p/>
11.<p>some text</p>
12.<h2>HH</h2>
13.</root>
<script type="text/javascript">
43.var data1 = {json:xml-to-json($xml1)};BaseX does not provide this and I wonder, what options I have and what would anyone recommend
1. building up plain text piece by piece by XQuery
2. using some Java lib for this (there are more, any practical experience welcome)
3. Some other trick, which lives hidden to me (I already queried BaseX wiki and no hit)
Jan
--
*Ing. Jan Vlčinský*
CAD programy
Slunečnicová 338/3, 734 01 Karviná Ráj, Czech Republic
tel: +420-597 602 024; mob: +420-608 979 040
skype: janvlcinsky; GoogleTalk: jan.vlcinsky at gmail.com
http://cz.linkedin.com/in/vlcinsky
MS
Michael Seiferle26 Jan 2011 21:43 #
Hi Jan,
for the moment we do not offer any options to export the resulting file in JSON. However it could easily be added when using our API.
I found
https://github.com/douglascrockford/JSON-java
which is a reference implementation according to http://www.json.org/java/ Converting serialized XML to JSON maybe adds another level of indirection but should suit most usecases well.
Hope this helps! In case it does not, feel free to ask for more.
Kind regards
Michael
Quoted text · Jan Vlčinský (CAD) · 50 lines
Am 26.01.2011 um 20:47 schrieb Jan Vlčinský (CAD): > Hi > I shall export JSON file based on XML structures I have in my XQuery. > Here I found http://demo.exist-db.org/xquery/json-test.xql/source sample, how eXistDB can produce JSON data. > > It is like > > import module namespace json="http://www.json.org"; > 04. > > 05. > declare option exist:serialize "method=xhtml media-type=text/html"; > > let $xml1 := > 08. > <root> > 09. > <h1>H</h1> > 10. > <p/> > 11. > <p>some text</p> > 12. > <h2>HH</h2> > 13. > </root> > > <script type="text/javascript"> > 43. > var data1 = {json:xml-to-json($xml1)}; > > BaseX does not provide this and I wonder, what options I have and what would anyone recommend > > • building up plain text piece by piece by XQuery > • using some Java lib for this (there are more, any practical experience welcome) > • Some other trick, which lives hidden to me (I already queried BaseX wiki and no hit) > Jan > > -- > Ing. Jan Vlčinský > CAD programy > Slunečnicová 338/3, 734 01 Karviná Ráj, Czech Republic > tel: +420-597 602 024; mob: +420-608 979 040 > skype: janvlcinsky; GoogleTalk: jan.vlcinsky at gmail.com > http://cz.linkedin.com/in/vlcinsky > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk
IL
Imsieke, Gerrit, le-tex27 Jan 2011 01:32 #
Jan,
If your infrastructure permits sending BaseX's output to an XSLT processor, this will be the most flexible and maintainable solution. (Of course you'll still have to write the XSLT templates, which is straightforward.)
There are many ways to express a given XML document in JSON. I implemented one of them below. But as I said, if you need to adapt it to your needs, it will be more costly to modify or branch this XQuery code than to maintain XSLT code. On the other hand, if no XSLT processor is available or if you need to write glue code, adding this additional piece of infrastructure is often also costly.
test.xq:
-----------------
declare function local:jsonify-node ($input as node()?, $indent as
xs:integer) as xs:string {
if (empty($input)) then ''
else
if ($input instance of element())
then concat(
local:indent($indent - 1), '{',
string-join(
(
concat(local:indent($indent), '"_name": "', name($input), '"'),
local:jsonify-attributes($input/@*, $indent),
if ($input/node())
then concat(
local:indent($indent), '"_children":',
local:indent($indent), "[",
string-join(for $n in $input/node() return
local:jsonify-node($n, $indent + 2), ', '),
local:indent($indent), "]"
)
else ()
),
','
),
local:indent($indent - 1), '}'
)
else concat(
local:indent($indent - 1), '{ "_text": ', '"', $input, '" }'
)
};
declare function local:jsonify-attributes ($input as attribute()*,
$indent as xs:integer) as xs:string? {
if ($input)
then string-join(
for $a in $input return concat(
local:indent($indent), '"', name($input), '": "', $input, '"'
),
','
)
else ()
};
declare function local:indent ($indent as xs:integer) as xs:string {
string-join(('
', for $i in (1 to 2 * $indent) return ' '))
};local:jsonify-node(collection('jsontest')/*, 1)
-----------------
test.xml:
-----------------
<?xml version="1.0"?>
<root>
<foo bar="4">
<a>Text <b>text</b></a>
</foo>
<foo/>
</root>
-----------------Then:
$ basex -c "create database jsontest; open jsontest; add test.xml"
$ basex test.xqResult:
-----------------
{
"_name": "root",
"_children":
[
{
"_name": "foo",
"bar": "4",
"_children":
[
{
"_name": "a",
"_children":
[
{ "_text": "Text" },
{
"_name": "b",
"_children":
[
{ "_text": "text" }
]
}
]
}
]
},
{
"_name": "foo"
}
]
}
-----------------Paste it into, e.g., http://json.parser.online.fr/
It will parse.
-Gerrit
Quoted text · Michael Seiferle · 73 lines
On 26.01.2011 21:43, Michael Seiferle wrote: > Hi Jan, > for the moment we do not offer any options to export the resulting file in JSON. > However it could easily be added when using our API. > > I found > https://github.com/douglascrockford/JSON-java > > which is a reference implementation according to http://www.json.org/java/ > Converting serialized XML to JSON maybe adds another level of indirection but should suit most usecases well. > > Hope this helps! In case it does not, feel free to ask for more. > > Kind regards > > Michael > > Am 26.01.2011 um 20:47 schrieb Jan Vlčinský (CAD): > >> Hi >> I shall export JSON file based on XML structures I have in my XQuery. >> Here I found http://demo.exist-db.org/xquery/json-test.xql/source sample, how eXistDB can produce JSON data. >> >> It is like >> >> import module namespace json="http://www.json.org"; >> 04. >> >> 05. >> declare option exist:serialize "method=xhtml media-type=text/html"; >> >> let $xml1 := >> 08. >> <root> >> 09. >> <h1>H</h1> >> 10. >> <p/> >> 11. >> <p>some text</p> >> 12. >> <h2>HH</h2> >> 13. >> </root> >> >> <script type="text/javascript"> >> 43. >> var data1 = {json:xml-to-json($xml1)}; >> >> BaseX does not provide this and I wonder, what options I have and what would anyone recommend >> >> • building up plain text piece by piece by XQuery >> • using some Java lib for this (there are more, any practical experience welcome) >> • Some other trick, which lives hidden to me (I already queried BaseX wiki and no hit) >> Jan >> >> -- >> Ing. Jan Vlčinský >> CAD programy >> Slunečnicová 338/3, 734 01 Karviná Ráj, Czech Republic >> tel: +420-597 602 024; mob: +420-608 979 040 >> skype: janvlcinsky; GoogleTalk: jan.vlcinsky at gmail.com >> http://cz.linkedin.com/in/vlcinsky >> >> _______________________________________________ >> BaseX-Talk mailing list >> BaseX-Talk at mailman.uni-konstanz.de >> https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk
--
Gerrit Imsieke
Geschäftsführer / Managing Director
le-tex publishing services GmbH
Weissenfelser Str. 84, 04229 Leipzig, Germany
Phone +49 341 355356 110, Fax +49 341 355356 510
gerrit.imsieke at le-tex.de, http://www.le-tex.de
Registergericht / Commercial Register: Amtsgericht Leipzig
Registernummer / Registration Number: HRB 24930
Geschäftsführer: Gerrit Imsieke, Svea Jelonek,
Thomas Schmidt, Dr. Reinhard Vöckler
JC
Jan Vlčinský (CAD)26 Feb 2011 13:31 #
Gerrit
Thanks for your hints and samples.
As I need the JSON file for SIMILE page, I found this link, which tries to resolve just this specific task:
http://en.wikibooks.org/wiki/XQuery/Simile_Exhibit
As SIMILE is expecting certain structure, one has to tailor the transformation for specific purpose.
But it is exactly the same as with "exporting to XML" :-)
I think, I will try to define some XML formating rules, transform my data into that and than run some standard XML to JSON transformation.
It is great to have some experts to discuss my tasks with.
Jan
2011/1/27 Imsieke, Gerrit, le-tex <gerrit.imsieke at le-tex.de>
Quoted text · Imsieke, Gerrit, le-tex · 223 lines
> Jan, > > If your infrastructure permits sending BaseX's output to an XSLT processor, > this will be the most flexible and maintainable solution. (Of course you'll > still have to write the XSLT templates, which is straightforward.) > > There are many ways to express a given XML document in JSON. I implemented > one of them below. But as I said, if you need to adapt it to your needs, it > will be more costly to modify or branch this XQuery code than to maintain > XSLT code. On the other hand, if no XSLT processor is available or if you > need to write glue code, adding this additional piece of infrastructure is > often also costly. > > > test.xq: > ----------------- > declare function local:jsonify-node ($input as node()?, $indent as > xs:integer) as xs:string { > if (empty($input)) then '' > else > if ($input instance of element()) > then concat( > local:indent($indent - 1), '{', > string-join( > ( > concat(local:indent($indent), '"_name": "', name($input), '"'), > local:jsonify-attributes($input/@*, $indent), > if ($input/node()) > then concat( > local:indent($indent), '"_children":', > local:indent($indent), "[", > string-join(for $n in $input/node() return > local:jsonify-node($n, $indent + 2), ', '), > local:indent($indent), "]" > ) > else () > ), > ',' > ), > local:indent($indent - 1), '}' > ) > else concat( > local:indent($indent - 1), '{ "_text": ', '"', $input, '" }' > ) > }; > > declare function local:jsonify-attributes ($input as attribute()*, $indent > as xs:integer) as xs:string? { > if ($input) > then string-join( > for $a in $input return concat( > local:indent($indent), '"', name($input), '": "', $input, '"' > ), > ',' > ) > else () > }; > > declare function local:indent ($indent as xs:integer) as xs:string { > string-join(('
', for $i in (1 to 2 * $indent) return ' ')) > }; > > local:jsonify-node(collection('jsontest')/*, 1) > ----------------- > > test.xml: > ----------------- > <?xml version="1.0"?> > <root> > <foo bar="4"> > <a>Text <b>text</b></a> > </foo> > <foo/> > </root> > ----------------- > > Then: > $ basex -c "create database jsontest; open jsontest; add test.xml" > $ basex test.xq > > Result: > ----------------- > { > "_name": "root", > "_children": > [ > { > "_name": "foo", > "bar": "4", > "_children": > [ > { > "_name": "a", > "_children": > [ > { "_text": "Text" }, > { > "_name": "b", > "_children": > [ > { "_text": "text" } > ] > } > ] > } > ] > }, > { > "_name": "foo" > } > ] > } > ----------------- > > Paste it into, e.g., http://json.parser.online.fr/ > It will parse. > > -Gerrit > > > > On 26.01.2011 21:43, Michael Seiferle wrote: > >> Hi Jan, >> for the moment we do not offer any options to export the resulting file in >> JSON. >> However it could easily be added when using our API. >> >> I found >> https://github.com/douglascrockford/JSON-java >> >> which is a reference implementation according to >> http://www.json.org/java/ >> Converting serialized XML to JSON maybe adds another level of indirection >> but should suit most usecases well. >> >> Hope this helps! In case it does not, feel free to ask for more. >> >> Kind regards >> >> Michael >> >> Am 26.01.2011 um 20:47 schrieb Jan Vlčinský (CAD): >> >> Hi >>> I shall export JSON file based on XML structures I have in my XQuery. >>> Here I found http://demo.exist-db.org/xquery/json-test.xql/sourcesample, how eXistDB can produce JSON data. >>> >>> It is like >>> >>> import module namespace json="http://www.json.org"; >>> 04. >>> >>> 05. >>> declare option exist:serialize "method=xhtml media-type=text/html"; >>> >>> let $xml1 := >>> 08. >>> <root> >>> 09. >>> <h1>H</h1> >>> 10. >>> <p/> >>> 11. >>> <p>some text</p> >>> 12. >>> <h2>HH</h2> >>> 13. >>> </root> >>> >>> <script type="text/javascript"> >>> 43. >>> var data1 = {json:xml-to-json($xml1)}; >>> >>> BaseX does not provide this and I wonder, what options I have and what >>> would anyone recommend >>> >>> • building up plain text piece by piece by XQuery >>> • using some Java lib for this (there are more, any practical >>> experience welcome) >>> • Some other trick, which lives hidden to me (I already queried >>> BaseX wiki and no hit) >>> Jan >>> >>> -- >>> Ing. Jan Vlčinský >>> CAD programy >>> Slunečnicová 338/3, 734 01 Karviná Ráj, Czech Republic >>> tel: +420-597 602 024; mob: +420-608 979 040 >>> skype: janvlcinsky; GoogleTalk: jan.vlcinsky at gmail.com >>> http://cz.linkedin.com/in/vlcinsky >>> >>> _______________________________________________ >>> BaseX-Talk mailing list >>> BaseX-Talk at mailman.uni-konstanz.de >>> https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >>> >> >> _______________________________________________ >> BaseX-Talk mailing list >> BaseX-Talk at mailman.uni-konstanz.de >> https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >> > > -- > Gerrit Imsieke > Geschäftsführer / Managing Director > le-tex publishing services GmbH > Weissenfelser Str. 84, 04229 Leipzig, Germany > Phone +49 341 355356 110, Fax +49 341 355356 510 > gerrit.imsieke at le-tex.de, http://www.le-tex.de > > Registergericht / Commercial Register: Amtsgericht Leipzig > Registernummer / Registration Number: HRB 24930 > > Geschäftsführer: Gerrit Imsieke, Svea Jelonek, > Thomas Schmidt, Dr. Reinhard Vöckler > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >
--
*Ing. Jan Vlčinský*
CAD programy
Slunečnicová 338/3, 734 01 Karviná Ráj, Czech Republic
tel: +420-597 602 024; mob: +420-608 979 040
skype: janvlcinsky; GoogleTalk: jan.vlcinsky at gmail.com
http://cz.linkedin.com/in/vlcinsky
Written for BaseX 6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery