Question around JSON Serialization
Friday, 2 August 2013 · 9 messages · last reply 3 Aug 2013
JW
James Wright2 Aug 2013 04:10 #
Hey BaseX Team,
Im trying to serialize some JSON in order to make a few calls to an external api. In the json I need to include 'html' for example: { "subject": "Test Subject", "content": { type: "text/html", text: "<div><h2>Issue:</h2></div> }, "type": "document"} When I call the following script however I get:
{ "subject": "Test Subject 10", "content": { "type": "text\/html", "text": "<html>\n <div>\n <h2>Issue:<\/h2>\n <\/div>\n<\/html>" }, "type": "document"} I tried messing with the serialization parameters however with no success. I use the resulting string as the content for a http post request. Any help would be appreciated. Script:let $htmlContent := <div><h2>Issue:</h2></div> return
json:serialize( <json objects="json"> <subject>Test Subject 10</subject> <content type="object"> <type>text/html</type> <text>{fn:serialize(<html>{ $someItem }</html>)}</text> </content> <type>document</type> </json> )
Thanks,JamesCG
Christian Grün2 Aug 2013 09:28 #
Hi James,
by default, all string results are XML-encoded. You’ll get the expected result by declaring "text" as output option in the prolog (header) of your query:
declare option output:method 'text';
let $someItem := ...Our Wiki page on serialization may give you more hints [1].
Hope this helps,
Christian
[1] http://docs.basex.org/wiki/Serialization
___________________________
2013/8/2 James Wright <james.jw at hotmail.com>:
Quoted text · James Wright · 50 lines
> Hey BaseX Team, > > Im trying to serialize some JSON in order to make a few calls to an external > api. In the json I need to include 'html' for example: > > { > "subject": "Test Subject", > "content": { > type: "text/html", > text: "<div><h2>Issue:</h2></div> > }, > "type": "document" > } > > When I call the following script however I get: > > { > "subject": "Test Subject 10", > "content": { > "type": "text\/html", > "text": "<html>\n <div>\n <h2>Issue:<\/h2>\n > <\/div>\n<\/html>" > }, > "type": "document" > } > > I tried messing with the serialization parameters however with no success. I > use the resulting string as the content for a http post request. Any help > would be appreciated. > > Script: > let $htmlContent := <div><h2>Issue:</h2></div> > return json:serialize( > <json objects="json"> > <subject>Test Subject 10</subject> > <content type="object"> > <type>text/html</type> > <text>{fn:serialize(<html>{ $someItem }</html>)}</text> > </content> > <type>document</type> > </json> ) > > Thanks, > James > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >
CG
Christian Grün2 Aug 2013 09:39 #
…one more thing: if your query is supposed to return all results as json, you can also use the "json" output method and get rid of the explicit conversion:
declare option output:method 'json';
let $someItem := '...'
let $htmlContent := <div><h2>Issue:</h2></div>
return <json objects="json">
<subject>Test Subject 10</subject>
<content type="object">
<type>text/html</type>
<text>{fn:serialize(<html>{ $someItem }</html>)}</text>
</content>
<type>document</type>
</json>
___________________________2013/8/2 Christian Grün <christian.gruen at gmail.com>:
Quoted text · Christian Grün · 68 lines
> Hi James, > > by default, all string results are XML-encoded. You’ll get the > expected result by declaring "text" as output option in the prolog > (header) of your query: > > declare option output:method 'text'; > let $someItem := ... > > Our Wiki page on serialization may give you more hints [1]. > > Hope this helps, > Christian > > [1] http://docs.basex.org/wiki/Serialization > ___________________________ > > 2013/8/2 James Wright <james.jw at hotmail.com>: >> Hey BaseX Team, >> >> Im trying to serialize some JSON in order to make a few calls to an external >> api. In the json I need to include 'html' for example: >> >> { >> "subject": "Test Subject", >> "content": { >> type: "text/html", >> text: "<div><h2>Issue:</h2></div> >> }, >> "type": "document" >> } >> >> When I call the following script however I get: >> >> { >> "subject": "Test Subject 10", >> "content": { >> "type": "text\/html", >> "text": "<html>\n <div>\n <h2>Issue:<\/h2>\n >> <\/div>\n<\/html>" >> }, >> "type": "document" >> } >> >> I tried messing with the serialization parameters however with no success. I >> use the resulting string as the content for a http post request. Any help >> would be appreciated. >> >> Script: >> let $htmlContent := <div><h2>Issue:</h2></div> >> return json:serialize( >> <json objects="json"> >> <subject>Test Subject 10</subject> >> <content type="object"> >> <type>text/html</type> >> <text>{fn:serialize(<html>{ $someItem }</html>)}</text> >> </content> >> <type>document</type> >> </json> ) >> >> Thanks, >> James >> >> _______________________________________________ >> BaseX-Talk mailing list >> BaseX-Talk at mailman.uni-konstanz.de >> https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >>
CG
Hi James,
could you please have another look at your example and provide us with some code that runs out of the box?
Thanks in advance,
Christian
___________________________
2013/8/2 James Wright <james.jw at hotmail.com>:
Quoted text · James Wright · 135 lines
> Hey Christian, > > Thank definitely did the trick however it affects the entire query. The > query should output xml of the api call requests. I tried adding > serialization parameters to the single fn:serialize method and set the > output to 'text' however now it only returns the 'text' inside of the > elements and not the xml. The script as a whole is supposed to return xml > with results. The only json part is for the API calls. Here is a more full > script: > > declare option db:parser "html"; > declare option db:htmlopt "html=true"; > declare variable $js := '{'; > declare variable $je := '}'; > let $serializationParameters := > <output:serialization-parameters > > xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization" > xmlns="http://example.org/ext"> > <output:method value="text"/> > > <results> > for $productInfo in > doc("http://www.someUrl.com/support/versions.shtml")//div[@id='contenedor']/div[@id] > return > let $request := > <http:request > href='http://www.someExternalService.com/api/core/v3/contents' > method='post' username='admin' password='admin' > send-authorization='true'> > <http:body media-type="application/json"> > {json:serialize( > <json objects="json"> > <subject>$productInfo/name</subject> > <content type="object"> > <type>text/html</type> > <text>{fn:serialize($productInfo/* , > $serializationParameters )}</text> > </content> > <type>document</type> > </json> )} > </http:body> > </http:request> return > <result> > try {http:send-request($request)[2]} > catch HC0001 {'Not Found'} > </result> > </results> > > I guess I would expect this to operate as you described since I set the > output:method to 'text'. Let me know if you have any ideas? I have read that > serialization page several times and I guess I'm just not understanding this > piece. > > Thanks again. > - James > >> From: christian.gruen at gmail.com >> Date: Fri, 2 Aug 2013 09:28:48 +0200 >> Subject: Re: [basex-talk] Question around JSON Serialization >> To: james.jw at hotmail.com >> CC: basex-talk at mailman.uni-konstanz.de > >> >> Hi James, >> >> by default, all string results are XML-encoded. You’ll get the >> expected result by declaring "text" as output option in the prolog >> (header) of your query: >> >> declare option output:method 'text'; >> let $someItem := ... >> >> Our Wiki page on serialization may give you more hints [1]. >> >> Hope this helps, >> Christian >> >> [1] http://docs.basex.org/wiki/Serialization >> ___________________________ >> >> 2013/8/2 James Wright <james.jw at hotmail.com>: >> > Hey BaseX Team, >> > >> > Im trying to serialize some JSON in order to make a few calls to an >> > external >> > api. In the json I need to include 'html' for example: >> > >> > { >> > "subject": "Test Subject", >> > "content": { >> > type: "text/html", >> > text: "<div><h2>Issue:</h2></div> >> > }, >> > "type": "document" >> > } >> > >> > When I call the following script however I get: >> > >> > { >> > "subject": "Test Subject 10", >> > "content": { >> > "type": "text\/html", >> > "text": "<html>\n <div>\n <h2>Issue:<\/h2>\n >> > <\/div>\n<\/html>" >> > }, >> > "type": "document" >> > } >> > >> > I tried messing with the serialization parameters however with no >> > success. I >> > use the resulting string as the content for a http post request. Any >> > help >> > would be appreciated. >> > >> > Script: >> > let $htmlContent := <div><h2>Issue:</h2></div> >> > return json:serialize( >> > <json objects="json"> >> > <subject>Test Subject 10</subject> >> > <content type="object"> >> > <type>text/html</type> >> > <text>{fn:serialize(<html>{ $someItem }</html>)}</text> >> > </content> >> > <type>document</type> >> > </json> ) >> > >> > Thanks, >> > James >> > >> > _______________________________________________ >> > BaseX-Talk mailing list >> > BaseX-Talk at mailman.uni-konstanz.de >> > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >> >
CG
I have attached a possible solution; don’t hesitate to tell us if you expect a different solution.
Best,
Christian
___________________________
declare option output:method 'json';
let $htmlContent := <div><h2>Issue:</h2></div>
return
<json objects="json">
<subject>Test Subject 10</subject>
<content type="object">
<type>text/html</type>
<text>{ fn:serialize($htmlContent) }</text>
</content>
<type>document</type>
</json>
___________________________2013/8/2 James Wright <james.jw at hotmail.com>:
Quoted text · James Wright · 206 lines
> Christian, > > Not a problem. Here is a script that perfectly demonstrates my issue and > should run on your end: > > declare variable $serializationParameters := > <output:serialization-parameters > xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization" > xmlns="http://example.org/ext"> > <output:method value="text"/> > </output:serialization-parameters>; > > let $htmlContent := <div><h2>Issue:</h2></div> > return json:serialize( > <json objects="json"> > <subject>Test Subject 10</subject> > <content type="object"> > <type>text/html</type> > <text>{fn:serialize($htmlContent, $serializationParameters > )}</text> > </content> > <type>document</type> > </json> ) > > This returns: > > { > "subject": "Test Subject 10", > "content": { > "type": "text\/html", > "text": "Issue:" > }, > "type": "document" > } > > However we need it to return: > > { > "subject": "Test Subject 10", > "content": { > "type": "text\/html", > "text": "<div><h2>Issue:<\/h2><\/div>" > }, > "type": "document" > } > > Thanks, > James > >> From: christian.gruen at gmail.com >> Date: Fri, 2 Aug 2013 17:32:11 +0200 > >> Subject: Re: [basex-talk] Question around JSON Serialization >> To: james.jw at hotmail.com >> CC: basex-talk at mailman.uni-konstanz.de >> >> Hi James, >> >> could you please have another look at your example and provide us with >> some code that runs out of the box? >> >> Thanks in advance, >> Christian >> ___________________________ >> >> 2013/8/2 James Wright <james.jw at hotmail.com>: >> > Hey Christian, >> > >> > Thank definitely did the trick however it affects the entire query. The >> > query should output xml of the api call requests. I tried adding >> > serialization parameters to the single fn:serialize method and set the >> > output to 'text' however now it only returns the 'text' inside of the >> > elements and not the xml. The script as a whole is supposed to return >> > xml >> > with results. The only json part is for the API calls. Here is a more >> > full >> > script: >> > >> > declare option db:parser "html"; >> > declare option db:htmlopt "html=true"; >> > declare variable $js := '{'; >> > declare variable $je := '}'; >> > let $serializationParameters := >> > <output:serialization-parameters >> > >> > xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization" >> > xmlns="http://example.org/ext"> >> > <output:method value="text"/> >> > >> > <results> >> > for $productInfo in >> > >> > doc("http://www.someUrl.com/support/versions.shtml")//div[@id='contenedor']/div[@id] >> > return >> > let $request := >> > <http:request >> > href='http://www.someExternalService.com/api/core/v3/contents' >> > method='post' username='admin' password='admin' >> > send-authorization='true'> >> > <http:body media-type="application/json"> >> > {json:serialize( >> > <json objects="json"> >> > <subject>$productInfo/name</subject> >> > <content type="object"> >> > <type>text/html</type> >> > <text>{fn:serialize($productInfo/* , >> > $serializationParameters )}</text> >> > </content> >> > <type>document</type> >> > </json> )} >> > </http:body> >> > </http:request> return >> > <result> >> > try {http:send-request($request)[2]} >> > catch HC0001 {'Not Found'} >> > </result> >> > </results> >> > >> > I guess I would expect this to operate as you described since I set the >> > output:method to 'text'. Let me know if you have any ideas? I have read >> > that >> > serialization page several times and I guess I'm just not understanding >> > this >> > piece. >> > >> > Thanks again. >> > - James >> > >> >> From: christian.gruen at gmail.com >> >> Date: Fri, 2 Aug 2013 09:28:48 +0200 >> >> Subject: Re: [basex-talk] Question around JSON Serialization >> >> To: james.jw at hotmail.com >> >> CC: basex-talk at mailman.uni-konstanz.de >> > >> >> >> >> Hi James, >> >> >> >> by default, all string results are XML-encoded. You’ll get the >> >> expected result by declaring "text" as output option in the prolog >> >> (header) of your query: >> >> >> >> declare option output:method 'text'; >> >> let $someItem := ... >> >> >> >> Our Wiki page on serialization may give you more hints [1]. >> >> >> >> Hope this helps, >> >> Christian >> >> >> >> [1] http://docs.basex.org/wiki/Serialization >> >> ___________________________ >> >> >> >> 2013/8/2 James Wright <james.jw at hotmail.com>: >> >> > Hey BaseX Team, >> >> > >> >> > Im trying to serialize some JSON in order to make a few calls to an >> >> > external >> >> > api. In the json I need to include 'html' for example: >> >> > >> >> > { >> >> > "subject": "Test Subject", >> >> > "content": { >> >> > type: "text/html", >> >> > text: "<div><h2>Issue:</h2></div> >> >> > }, >> >> > "type": "document" >> >> > } >> >> > >> >> > When I call the following script however I get: >> >> > >> >> > { >> >> > "subject": "Test Subject 10", >> >> > "content": { >> >> > "type": "text\/html", >> >> > "text": "<html>\n <div>\n <h2>Issue:<\/h2>\n >> >> > <\/div>\n<\/html>" >> >> > }, >> >> > "type": "document" >> >> > } >> >> > >> >> > I tried messing with the serialization parameters however with no >> >> > success. I >> >> > use the resulting string as the content for a http post request. Any >> >> > help >> >> > would be appreciated. >> >> > >> >> > Script: >> >> > let $htmlContent := <div><h2>Issue:</h2></div> >> >> > return json:serialize( >> >> > <json objects="json"> >> >> > <subject>Test Subject 10</subject> >> >> > <content type="object"> >> >> > <type>text/html</type> >> >> > <text>{fn:serialize(<html>{ $someItem }</html>)}</text> >> >> > </content> >> >> > <type>document</type> >> >> > </json> ) >> >> > >> >> > Thanks, >> >> > James >> >> > >> >> > _______________________________________________ >> >> > BaseX-Talk mailing list >> >> > BaseX-Talk at mailman.uni-konstanz.de >> >> > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >> >> >
JW
Hey Christian,
Thank definitely did the trick however it affects the entire query. The query should output xml of the api call requests. I tried adding serialization parameters to the single
fn:serialize method and set the output to 'text' however now it only returns the 'text' inside of the elements and not the xml. The script as a whole is supposed to return xml with results. The only json part is for the API calls. Here is a more full script: declare option db:parser "html";declare option db:htmlopt "html=true";declare variable $js := '{';declare variable $je := '}';let $serializationParameters := <output:serialization-parameters xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization" xmlns="http://example.org/ext"> <output:method value="text"/> <results> for $productInfo in doc("http://www.someUrl.com/support/versions.shtml")//div[@id='contenedor']/div[@id] return let $request := <http:request href='http://www.someExternalService.com/api/core/v3/contents' method='post' username='admin' password='admin' send-authorization='true'> <http:body media-type="application/json"> {json:serialize( <json objects="json"> <subject>$productInfo/name</subject> <content type="object"> <type>text/html</type> <text>{fn:serialize($productInfo/* , $serializationParameters )}</text> </content> <type>document</type> </json> )} </http:body> </http:request> return <result> try {http:send-request($request)[2]} catch HC0001 {'Not Found'} </result> </results> I guess I would expect this to operate as you described since I set the output:method to 'text'. Let me know if you have any ideas? I have read that serialization page several times and I guess I'm just not understanding this piece. Thanks again. - JamesQuoted text · Christian Grün · 74 lines
> From: christian.gruen at gmail.com > Date: Fri, 2 Aug 2013 09:28:48 +0200 > Subject: Re: [basex-talk] Question around JSON Serialization > To: james.jw at hotmail.com > CC: basex-talk at mailman.uni-konstanz.de > > Hi James, > > by default, all string results are XML-encoded. You’ll get the > expected result by declaring "text" as output option in the prolog > (header) of your query: > > declare option output:method 'text'; > let $someItem := ... > > Our Wiki page on serialization may give you more hints [1]. > > Hope this helps, > Christian > > [1] http://docs.basex.org/wiki/Serialization > ___________________________ > > 2013/8/2 James Wright <james.jw at hotmail.com>: > > Hey BaseX Team, > > > > Im trying to serialize some JSON in order to make a few calls to an external > > api. In the json I need to include 'html' for example: > > > > { > > "subject": "Test Subject", > > "content": { > > type: "text/html", > > text: "<div><h2>Issue:</h2></div> > > }, > > "type": "document" > > } > > > > When I call the following script however I get: > > > > { > > "subject": "Test Subject 10", > > "content": { > > "type": "text\/html", > > "text": "<html>\n <div>\n <h2>Issue:<\/h2>\n > > <\/div>\n<\/html>" > > }, > > "type": "document" > > } > > > > I tried messing with the serialization parameters however with no success. I > > use the resulting string as the content for a http post request. Any help > > would be appreciated. > > > > Script: > > let $htmlContent := <div><h2>Issue:</h2></div> > > return json:serialize( > > <json objects="json"> > > <subject>Test Subject 10</subject> > > <content type="object"> > > <type>text/html</type> > > <text>{fn:serialize(<html>{ $someItem }</html>)}</text> > > </content> > > <type>document</type> > > </json> ) > > > > Thanks, > > James > > > > _______________________________________________ > > BaseX-Talk mailing list > > BaseX-Talk at mailman.uni-konstanz.de > > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk > >
JW
Christian,
Not a problem. Here is a script that perfectly demonstrates my issue and should run on your end: declare variable $serializationParameters := <output:serialization-parameters xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization" xmlns="http://example.org/ext"> <output:method value="text"/> </output:serialization-parameters>; let $htmlContent := <div><h2>Issue:</h2></div> return
json:serialize( <json objects="json"> <subject>Test Subject 10</subject> <content type="object"> <type>text/html</type> <text>{fn:serialize($htmlContent, $serializationParameters )}</text> </content> <type>document</type> </json> ) This returns:
{ "subject": "Test Subject 10", "content": { "type": "text\/html", "text": "Issue:" }, "type": "document"} However we need it to return:
{ "subject": "Test Subject 10", "content": { "type": "text\/html", "text": "<div><h2>Issue:<\/h2><\/div>" }, "type": "document"}
Thanks,JamesQuoted text · Christian Grün · 151 lines
> From: christian.gruen at gmail.com > Date: Fri, 2 Aug 2013 17:32:11 +0200 > Subject: Re: [basex-talk] Question around JSON Serialization > To: james.jw at hotmail.com > CC: basex-talk at mailman.uni-konstanz.de > > Hi James, > > could you please have another look at your example and provide us with > some code that runs out of the box? > > Thanks in advance, > Christian > ___________________________ > > 2013/8/2 James Wright <james.jw at hotmail.com>: > > Hey Christian, > > > > Thank definitely did the trick however it affects the entire query. The > > query should output xml of the api call requests. I tried adding > > serialization parameters to the single fn:serialize method and set the > > output to 'text' however now it only returns the 'text' inside of the > > elements and not the xml. The script as a whole is supposed to return xml > > with results. The only json part is for the API calls. Here is a more full > > script: > > > > declare option db:parser "html"; > > declare option db:htmlopt "html=true"; > > declare variable $js := '{'; > > declare variable $je := '}'; > > let $serializationParameters := > > <output:serialization-parameters > > > > xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization" > > xmlns="http://example.org/ext"> > > <output:method value="text"/> > > > > <results> > > for $productInfo in > > doc("http://www.someUrl.com/support/versions.shtml")//div[@id='contenedor']/div[@id] > > return > > let $request := > > <http:request > > href='http://www.someExternalService.com/api/core/v3/contents' > > method='post' username='admin' password='admin' > > send-authorization='true'> > > <http:body media-type="application/json"> > > {json:serialize( > > <json objects="json"> > > <subject>$productInfo/name</subject> > > <content type="object"> > > <type>text/html</type> > > <text>{fn:serialize($productInfo/* , > > $serializationParameters )}</text> > > </content> > > <type>document</type> > > </json> )} > > </http:body> > > </http:request> return > > <result> > > try {http:send-request($request)[2]} > > catch HC0001 {'Not Found'} > > </result> > > </results> > > > > I guess I would expect this to operate as you described since I set the > > output:method to 'text'. Let me know if you have any ideas? I have read that > > serialization page several times and I guess I'm just not understanding this > > piece. > > > > Thanks again. > > - James > > > >> From: christian.gruen at gmail.com > >> Date: Fri, 2 Aug 2013 09:28:48 +0200 > >> Subject: Re: [basex-talk] Question around JSON Serialization > >> To: james.jw at hotmail.com > >> CC: basex-talk at mailman.uni-konstanz.de > > > >> > >> Hi James, > >> > >> by default, all string results are XML-encoded. You’ll get the > >> expected result by declaring "text" as output option in the prolog > >> (header) of your query: > >> > >> declare option output:method 'text'; > >> let $someItem := ... > >> > >> Our Wiki page on serialization may give you more hints [1]. > >> > >> Hope this helps, > >> Christian > >> > >> [1] http://docs.basex.org/wiki/Serialization > >> ___________________________ > >> > >> 2013/8/2 James Wright <james.jw at hotmail.com>: > >> > Hey BaseX Team, > >> > > >> > Im trying to serialize some JSON in order to make a few calls to an > >> > external > >> > api. In the json I need to include 'html' for example: > >> > > >> > { > >> > "subject": "Test Subject", > >> > "content": { > >> > type: "text/html", > >> > text: "<div><h2>Issue:</h2></div> > >> > }, > >> > "type": "document" > >> > } > >> > > >> > When I call the following script however I get: > >> > > >> > { > >> > "subject": "Test Subject 10", > >> > "content": { > >> > "type": "text\/html", > >> > "text": "<html>\n <div>\n <h2>Issue:<\/h2>\n > >> > <\/div>\n<\/html>" > >> > }, > >> > "type": "document" > >> > } > >> > > >> > I tried messing with the serialization parameters however with no > >> > success. I > >> > use the resulting string as the content for a http post request. Any > >> > help > >> > would be appreciated. > >> > > >> > Script: > >> > let $htmlContent := <div><h2>Issue:</h2></div> > >> > return json:serialize( > >> > <json objects="json"> > >> > <subject>Test Subject 10</subject> > >> > <content type="object"> > >> > <type>text/html</type> > >> > <text>{fn:serialize(<html>{ $someItem }</html>)}</text> > >> > </content> > >> > <type>document</type> > >> > </json> ) > >> > > >> > Thanks, > >> > James > >> > > >> > _______________________________________________ > >> > BaseX-Talk mailing list > >> > BaseX-Talk at mailman.uni-konstanz.de > >> > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk > >> >
JW
Christian,
Thanks for the help. This works, but as with the other method. It affects the entire script which is not desired. I just need the call to
fn:serialize to create json. No where else. Once I set this I have to adjust a lot of the methods i'm using to accept json instead of xml since thats what 90% of our stuff works with. Can you change the output mid script? The output is xml, i just need the json for mapping an http request. For example: declare option db:parser "html";declare option db:htmlopt "html=true"; for $product in doc("http://www.telvent-gis.com/support/versions.shtml")//div[@id='contenedor']/div[@id] return let $request := <http:request href='http://localhost:8080/api/core/v3/contents' method='post' username='admin' password='admin' send-authorization='true'> <http:body media-type="application/json"> { json:serialize( <json objects="json"> <subject>Test Subject 22</subject> <content type="object"> <type>text/html</type> <text>{fn:serialize($product/*)}</text> </content> <type>document</type> </json>) } </http:body> </http:request> return try { http:send-request($request) } catch HC0001 { 'Not Found' } Thanks again for spending time on this. I might need to look into some other method. :(- JamesQuoted text · Christian Grün · 234 lines
> From: christian.gruen at gmail.com > Date: Fri, 2 Aug 2013 18:01:13 +0200 > Subject: Re: [basex-talk] Question around JSON Serialization > To: james.jw at hotmail.com > CC: basex-talk at mailman.uni-konstanz.de > > I have attached a possible solution; don’t hesitate to tell us if you > expect a different solution. > > Best, > Christian > ___________________________ > > declare option output:method 'json'; > > let $htmlContent := <div><h2>Issue:</h2></div> > return > <json objects="json"> > <subject>Test Subject 10</subject> > <content type="object"> > <type>text/html</type> > <text>{ fn:serialize($htmlContent) }</text> > </content> > <type>document</type> > </json> > ___________________________ > > 2013/8/2 James Wright <james.jw at hotmail.com>: > > Christian, > > > > Not a problem. Here is a script that perfectly demonstrates my issue and > > should run on your end: > > > > declare variable $serializationParameters := > > <output:serialization-parameters > > xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization" > > xmlns="http://example.org/ext"> > > <output:method value="text"/> > > </output:serialization-parameters>; > > > > let $htmlContent := <div><h2>Issue:</h2></div> > > return json:serialize( > > <json objects="json"> > > <subject>Test Subject 10</subject> > > <content type="object"> > > <type>text/html</type> > > <text>{fn:serialize($htmlContent, $serializationParameters > > )}</text> > > </content> > > <type>document</type> > > </json> ) > > > > This returns: > > > > { > > "subject": "Test Subject 10", > > "content": { > > "type": "text\/html", > > "text": "Issue:" > > }, > > "type": "document" > > } > > > > However we need it to return: > > > > { > > "subject": "Test Subject 10", > > "content": { > > "type": "text\/html", > > "text": "<div><h2>Issue:<\/h2><\/div>" > > }, > > "type": "document" > > } > > > > Thanks, > > James > > > >> From: christian.gruen at gmail.com > >> Date: Fri, 2 Aug 2013 17:32:11 +0200 > > > >> Subject: Re: [basex-talk] Question around JSON Serialization > >> To: james.jw at hotmail.com > >> CC: basex-talk at mailman.uni-konstanz.de > >> > >> Hi James, > >> > >> could you please have another look at your example and provide us with > >> some code that runs out of the box? > >> > >> Thanks in advance, > >> Christian > >> ___________________________ > >> > >> 2013/8/2 James Wright <james.jw at hotmail.com>: > >> > Hey Christian, > >> > > >> > Thank definitely did the trick however it affects the entire query. The > >> > query should output xml of the api call requests. I tried adding > >> > serialization parameters to the single fn:serialize method and set the > >> > output to 'text' however now it only returns the 'text' inside of the > >> > elements and not the xml. The script as a whole is supposed to return > >> > xml > >> > with results. The only json part is for the API calls. Here is a more > >> > full > >> > script: > >> > > >> > declare option db:parser "html"; > >> > declare option db:htmlopt "html=true"; > >> > declare variable $js := '{'; > >> > declare variable $je := '}'; > >> > let $serializationParameters := > >> > <output:serialization-parameters > >> > > >> > xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization" > >> > xmlns="http://example.org/ext"> > >> > <output:method value="text"/> > >> > > >> > <results> > >> > for $productInfo in > >> > > >> > doc("http://www.someUrl.com/support/versions.shtml")//div[@id='contenedor']/div[@id] > >> > return > >> > let $request := > >> > <http:request > >> > href='http://www.someExternalService.com/api/core/v3/contents' > >> > method='post' username='admin' password='admin' > >> > send-authorization='true'> > >> > <http:body media-type="application/json"> > >> > {json:serialize( > >> > <json objects="json"> > >> > <subject>$productInfo/name</subject> > >> > <content type="object"> > >> > <type>text/html</type> > >> > <text>{fn:serialize($productInfo/* , > >> > $serializationParameters )}</text> > >> > </content> > >> > <type>document</type> > >> > </json> )} > >> > </http:body> > >> > </http:request> return > >> > <result> > >> > try {http:send-request($request)[2]} > >> > catch HC0001 {'Not Found'} > >> > </result> > >> > </results> > >> > > >> > I guess I would expect this to operate as you described since I set the > >> > output:method to 'text'. Let me know if you have any ideas? I have read > >> > that > >> > serialization page several times and I guess I'm just not understanding > >> > this > >> > piece. > >> > > >> > Thanks again. > >> > - James > >> > > >> >> From: christian.gruen at gmail.com > >> >> Date: Fri, 2 Aug 2013 09:28:48 +0200 > >> >> Subject: Re: [basex-talk] Question around JSON Serialization > >> >> To: james.jw at hotmail.com > >> >> CC: basex-talk at mailman.uni-konstanz.de > >> > > >> >> > >> >> Hi James, > >> >> > >> >> by default, all string results are XML-encoded. You’ll get the > >> >> expected result by declaring "text" as output option in the prolog > >> >> (header) of your query: > >> >> > >> >> declare option output:method 'text'; > >> >> let $someItem := ... > >> >> > >> >> Our Wiki page on serialization may give you more hints [1]. > >> >> > >> >> Hope this helps, > >> >> Christian > >> >> > >> >> [1] http://docs.basex.org/wiki/Serialization > >> >> ___________________________ > >> >> > >> >> 2013/8/2 James Wright <james.jw at hotmail.com>: > >> >> > Hey BaseX Team, > >> >> > > >> >> > Im trying to serialize some JSON in order to make a few calls to an > >> >> > external > >> >> > api. In the json I need to include 'html' for example: > >> >> > > >> >> > { > >> >> > "subject": "Test Subject", > >> >> > "content": { > >> >> > type: "text/html", > >> >> > text: "<div><h2>Issue:</h2></div> > >> >> > }, > >> >> > "type": "document" > >> >> > } > >> >> > > >> >> > When I call the following script however I get: > >> >> > > >> >> > { > >> >> > "subject": "Test Subject 10", > >> >> > "content": { > >> >> > "type": "text\/html", > >> >> > "text": "<html>\n <div>\n <h2>Issue:<\/h2>\n > >> >> > <\/div>\n<\/html>" > >> >> > }, > >> >> > "type": "document" > >> >> > } > >> >> > > >> >> > I tried messing with the serialization parameters however with no > >> >> > success. I > >> >> > use the resulting string as the content for a http post request. Any > >> >> > help > >> >> > would be appreciated. > >> >> > > >> >> > Script: > >> >> > let $htmlContent := <div><h2>Issue:</h2></div> > >> >> > return json:serialize( > >> >> > <json objects="json"> > >> >> > <subject>Test Subject 10</subject> > >> >> > <content type="object"> > >> >> > <type>text/html</type> > >> >> > <text>{fn:serialize(<html>{ $someItem }</html>)}</text> > >> >> > </content> > >> >> > <type>document</type> > >> >> > </json> ) > >> >> > > >> >> > Thanks, > >> >> > James > >> >> > > >> >> > _______________________________________________ > >> >> > BaseX-Talk mailing list > >> >> > BaseX-Talk at mailman.uni-konstanz.de > >> >> > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk > >> >> >
CG
Christian Grün3 Aug 2013 16:17 #
Hi James,
it may be worthwhile to point out what serialization means in the context of XQuery:
- on the one hand, we have the internal representation of XQuery values, in which special characters like angle brackets are stored as single characters.
- if we want to output results as text, all values are serialized. In this step, if the "xml" default output method is used, angle characters that occur in strings are encoded to entities. However, entity encoding won't take place if you different things with your results, such as storing them in a database, sending it to a remote source, etc.
I don't know if this information helps?
What’s the response of the query you attached in your last mail?
Christian
Quoted text · James Wright · 290 lines
Am 02.08.2013 19:32 schrieb "James Wright" <james.jw at hotmail.com>: > Christian, > > Thanks for the help. This works, but as with the other method. It affects > the entire script which is not desired. I just need the call to > fn:serialize to create json. No where else. > > Once I set this I have to adjust a lot of the methods i'm using to accept > json instead of xml since thats what 90% of our stuff works with. Can you > change the output mid script? The output is xml, i just need the json for > mapping an http request. For example: > > declare option db:parser "html"; > declare option db:htmlopt "html=true"; > > for $product in doc("http://www.telvent-gis.com/support/versions.shtml")//div[@id='contenedor']/div[@id] > return > let $request := > <http:request href='http://localhost:8080/api/core/v3/contents' > method='post' username='admin' password='admin' > send-authorization='true'> > <http:body media-type="application/json"> > { > * json:serialize(* > * <json objects="json">* > * <subject>Test Subject 22</subject>* > * <content type="object">* > * <type>text/html</type>* > * <text>{fn:serialize($product/*)}</text>* > * </content>* > * <type>document</type>* > * </json>)* } > </http:body> > </http:request> return > try { http:send-request($request) } > catch HC0001 { 'Not Found' } > > Thanks again for spending time on this. I might need to look into some > other method. :( > - James > > > > From: christian.gruen at gmail.com > > Date: Fri, 2 Aug 2013 18:01:13 +0200 > > Subject: Re: [basex-talk] Question around JSON Serialization > > To: james.jw at hotmail.com > > CC: basex-talk at mailman.uni-konstanz.de > > > > I have attached a possible solution; don’t hesitate to tell us if you > > expect a different solution. > > > > Best, > > Christian > > ___________________________ > > > > declare option output:method 'json'; > > > > let $htmlContent := <div><h2>Issue:</h2></div> > > return > > <json objects="json"> > > <subject>Test Subject 10</subject> > > <content type="object"> > > <type>text/html</type> > > <text>{ fn:serialize($htmlContent) }</text> > > </content> > > <type>document</type> > > </json> > > ___________________________ > > > > 2013/8/2 James Wright <james.jw at hotmail.com>: > > > Christian, > > > > > > Not a problem. Here is a script that perfectly demonstrates my issue > and > > > should run on your end: > > > > > > declare variable $serializationParameters := > > > <output:serialization-parameters > > > xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization" > > > xmlns="http://example.org/ext"> > > > <output:method value="text"/> > > > </output:serialization-parameters>; > > > > > > let $htmlContent := <div><h2>Issue:</h2></div> > > > return json:serialize( > > > <json objects="json"> > > > <subject>Test Subject 10</subject> > > > <content type="object"> > > > <type>text/html</type> > > > <text>{fn:serialize($htmlContent, $serializationParameters > > > )}</text> > > > </content> > > > <type>document</type> > > > </json> ) > > > > > > This returns: > > > > > > { > > > "subject": "Test Subject 10", > > > "content": { > > > "type": "text\/html", > > > "text": "Issue:" > > > }, > > > "type": "document" > > > } > > > > > > However we need it to return: > > > > > > { > > > "subject": "Test Subject 10", > > > "content": { > > > "type": "text\/html", > > > "text": "<div><h2>Issue:<\/h2><\/div>" > > > }, > > > "type": "document" > > > } > > > > > > Thanks, > > > James > > > > > >> From: christian.gruen at gmail.com > > >> Date: Fri, 2 Aug 2013 17:32:11 +0200 > > > > > >> Subject: Re: [basex-talk] Question around JSON Serialization > > >> To: james.jw at hotmail.com > > >> CC: basex-talk at mailman.uni-konstanz.de > > >> > > >> Hi James, > > >> > > >> could you please have another look at your example and provide us with > > >> some code that runs out of the box? > > >> > > >> Thanks in advance, > > >> Christian > > >> ___________________________ > > >> > > >> 2013/8/2 James Wright <james.jw at hotmail.com>: > > >> > Hey Christian, > > >> > > > >> > Thank definitely did the trick however it affects the entire query. > The > > >> > query should output xml of the api call requests. I tried adding > > >> > serialization parameters to the single fn:serialize method and set > the > > >> > output to 'text' however now it only returns the 'text' inside of > the > > >> > elements and not the xml. The script as a whole is supposed to > return > > >> > xml > > >> > with results. The only json part is for the API calls. Here is a > more > > >> > full > > >> > script: > > >> > > > >> > declare option db:parser "html"; > > >> > declare option db:htmlopt "html=true"; > > >> > declare variable $js := '{'; > > >> > declare variable $je := '}'; > > >> > let $serializationParameters := > > >> > <output:serialization-parameters > > >> > > > >> > xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization" > > >> > xmlns="http://example.org/ext"> > > >> > <output:method value="text"/> > > >> > > > >> > <results> > > >> > for $productInfo in > > >> > > > >> > doc("http://www.someUrl.com/support/versions.shtml > ")//div[@id='contenedor']/div[@id] > > >> > return > > >> > let $request := > > >> > <http:request > > >> > href='http://www.someExternalService.com/api/core/v3/contents' > > >> > method='post' username='admin' password='admin' > > >> > send-authorization='true'> > > >> > <http:body media-type="application/json"> > > >> > {json:serialize( > > >> > <json objects="json"> > > >> > <subject>$productInfo/name</subject> > > >> > <content type="object"> > > >> > <type>text/html</type> > > >> > <text>{fn:serialize($productInfo/* , > > >> > $serializationParameters )}</text> > > >> > </content> > > >> > <type>document</type> > > >> > </json> )} > > >> > </http:body> > > >> > </http:request> return > > >> > <result> > > >> > try {http:send-request($request)[2]} > > >> > catch HC0001 {'Not Found'} > > >> > </result> > > >> > </results> > > >> > > > >> > I guess I would expect this to operate as you described since I set > the > > >> > output:method to 'text'. Let me know if you have any ideas? I have > read > > >> > that > > >> > serialization page several times and I guess I'm just not > understanding > > >> > this > > >> > piece. > > >> > > > >> > Thanks again. > > >> > - James > > >> > > > >> >> From: christian.gruen at gmail.com > > >> >> Date: Fri, 2 Aug 2013 09:28:48 +0200 > > >> >> Subject: Re: [basex-talk] Question around JSON Serialization > > >> >> To: james.jw at hotmail.com > > >> >> CC: basex-talk at mailman.uni-konstanz.de > > >> > > > >> >> > > >> >> Hi James, > > >> >> > > >> >> by default, all string results are XML-encoded. You’ll get the > > >> >> expected result by declaring "text" as output option in the prolog > > >> >> (header) of your query: > > >> >> > > >> >> declare option output:method 'text'; > > >> >> let $someItem := ... > > >> >> > > >> >> Our Wiki page on serialization may give you more hints [1]. > > >> >> > > >> >> Hope this helps, > > >> >> Christian > > >> >> > > >> >> [1] http://docs.basex.org/wiki/Serialization > > >> >> ___________________________ > > >> >> > > >> >> 2013/8/2 James Wright <james.jw at hotmail.com>: > > >> >> > Hey BaseX Team, > > >> >> > > > >> >> > Im trying to serialize some JSON in order to make a few calls to > an > > >> >> > external > > >> >> > api. In the json I need to include 'html' for example: > > >> >> > > > >> >> > { > > >> >> > "subject": "Test Subject", > > >> >> > "content": { > > >> >> > type: "text/html", > > >> >> > text: "<div><h2>Issue:</h2></div> > > >> >> > }, > > >> >> > "type": "document" > > >> >> > } > > >> >> > > > >> >> > When I call the following script however I get: > > >> >> > > > >> >> > { > > >> >> > "subject": "Test Subject 10", > > >> >> > "content": { > > >> >> > "type": "text\/html", > > >> >> > "text": "<html>\n <div>\n > <h2>Issue:<\/h2>\n > > >> >> > <\/div>\n<\/html>" > > >> >> > }, > > >> >> > "type": "document" > > >> >> > } > > >> >> > > > >> >> > I tried messing with the serialization parameters however with no > > >> >> > success. I > > >> >> > use the resulting string as the content for a http post request. > Any > > >> >> > help > > >> >> > would be appreciated. > > >> >> > > > >> >> > Script: > > >> >> > let $htmlContent := <div><h2>Issue:</h2></div> > > >> >> > return json:serialize( > > >> >> > <json objects="json"> > > >> >> > <subject>Test Subject 10</subject> > > >> >> > <content type="object"> > > >> >> > <type>text/html</type> > > >> >> > <text>{fn:serialize(<html>{ $someItem }</html>)}</text> > > >> >> > </content> > > >> >> > <type>document</type> > > >> >> > </json> ) > > >> >> > > > >> >> > Thanks, > > >> >> > James > > >> >> > > > >> >> > _______________________________________________ > > >> >> > BaseX-Talk mailing list > > >> >> > BaseX-Talk at mailman.uni-konstanz.de > > >> >> > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk > > >> >> > >
Written for BaseX 7.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery