Single quotes with json::parse
Thursday, 10 August 2017 · 4 messages
TD
Thomas Daly10 Aug 2017 14:41 #
Regarding the JSON module json::parse function, how do you cope with single quotes within the JSON data?
I have tried escaping them with a backslash, but to no avail. In this example I think it's the single quote in "St. John's College" that is causing the problem:
XQUERY let $database := 'lk'
let $options := map { 'format': 'direct', 'escape':
'yes' } let $j :=
json:parse('{"roles":["poet","cleric"],"institutions":["St John's College","Cambridge
University"],"characterId":"595051204300","homes":["Cheapside","London","Dea n
Prior","Devon"],"image":"robert_herrick.jpg","DOB":"1591-08-24","attributes" :["People educated at Merchant Taylors' School","Northwood","Alumni of St John's College","Cambridge","Alumni of Trinity Hall","Cambridge","English poets","People from the City of London","People educated at Westminster School","London","English male
poets"],"DOD":"1674-10-08","nationality":"British","givenName":"Robert","fam ilyName":"Herrick"}', $options)
return db:add($database, $j,
'characters/c_595051204300_Robert_Herrick.xml')
Stopped at ., 3/78:
[XPST0003] Expecting closing bracket: json:parse. at
/usr/local/lib/basex-api/src/main/perl/BaseXClient.pm line 58.
Many thanks,
ThomasCG
Christian Grün10 Aug 2017 14:45 #
Hi Thomas,
This problem is a general one when working with quotes; see:
'It is called "St. John's College"!'
One solution is to go with the XQuery 3.1 string constructor [1]
``[It is called "St. John's College"!]``
Cheers,
Christian
[1] http://docs.basex.org/wiki/XQuery_3.1#String_Constructor
Quoted text · Thomas Daly · 46 lines
On Thu, Aug 10, 2017 at 2:41 PM, Thomas Daly <letterbox at daladi.com> wrote: > Regarding the JSON module json::parse function, how do you cope with single > quotes within the JSON data? > > > > I have tried escaping them with a backslash, but to no avail. In this > example I think it’s the single quote in “St. John’s College” that is > causing the problem: > > > > XQUERY let $database := 'lk' > > let $options := map { 'format': 'direct', 'escape': > 'yes' } > > let $j := > json:parse('{"roles":["poet","cleric"],"institutions":["St John's > College","Cambridge > University"],"characterId":"595051204300","homes":["Cheapside","London","Dean > Prior","Devon"],"image":"robert_herrick.jpg","DOB":"1591-08-24","attributes":["People > educated at Merchant Taylors' School","Northwood","Alumni of St John's > College","Cambridge","Alumni of Trinity Hall","Cambridge","English > poets","People from the City of London","People educated at Westminster > School","London","English male > poets"],"DOD":"1674-10-08","nationality":"British","givenName":"Robert","familyName":"Herrick"}', > $options) > > return db:add($database, $j, > 'characters/c_595051204300_Robert_Herrick.xml') > > > > Stopped at ., 3/78: > > [XPST0003] Expecting closing bracket: json:parse. at > /usr/local/lib/basex-api/src/main/perl/BaseXClient.pm line 58. > > > > Many thanks, > > Thomas > >
SA
Just to make sure: Have you also escaped Merchant Taylors' School at the same time?
I've successfully tried ' to escape the apostrophe.
Quoted text · Thomas Daly · 47 lines
On 10.08.2017 14:41, Thomas Daly wrote: > Regarding the JSON module json::parse function, how do you cope with > single quotes within the JSON data? > > > > I have tried escaping them with a backslash, but to no avail. In this > example I think it’s the single quote in “St. John’s College” that is > causing the problem: > > > > XQUERY let $database := 'lk' > > let $options := map { 'format': 'direct', > 'escape': 'yes' } > > let $j := > json:parse('{"roles":["poet","cleric"],"institutions":["St John's > College","Cambridge > University"],"characterId":"595051204300","homes":["Cheapside","London","Dean > Prior","Devon"],"image":"robert_herrick.jpg","DOB":"1591-08-24","attributes":["People > educated at Merchant Taylors' School","Northwood","Alumni of St John's > College","Cambridge","Alumni of Trinity Hall","Cambridge","English > poets","People from the City of London","People educated at Westminster > School","London","English male > poets"],"DOD":"1674-10-08","nationality":"British","givenName":"Robert","familyName":"Herrick"}', > $options) > > return db:add($database, $j, > 'characters/c_595051204300_Robert_Herrick.xml') > > > > Stopped at ., 3/78: > > [XPST0003] Expecting closing bracket: json:parse. at > /usr/local/lib/basex-api/src/main/perl/BaseXClient.pm line 58. > > > > Many thanks, > > Thomas > > >
TD
Thomas Daly10 Aug 2017 15:03 #
Hello,
Yes, I did escape all of them but got the same error.
I have tried Christian's solution which solves the problem, and good to know about ' too.
Many thanks,
Thomas
Quoted text · Sebastian Albert · 59 lines
-----Original Message----- From: basex-talk-bounces at mailman.uni-konstanz.de [mailto:basex-talk-bounces at mailman.uni-konstanz.de] On Behalf Of Sebastian Albert Sent: 10 August 2017 13:47 To: basex-talk at mailman.uni-konstanz.de Subject: Re: [basex-talk] Single quotes with json::parse Just to make sure: Have you also escaped Merchant Taylors' School at the same time? I've successfully tried ' to escape the apostrophe. On 10.08.2017 14:41, Thomas Daly wrote: > Regarding the JSON module json::parse function, how do you cope with > single quotes within the JSON data? > > > > I have tried escaping them with a backslash, but to no avail. In this > example I think it's the single quote in "St. John's College" that is > causing the problem: > > > > XQUERY let $database := 'lk' > > let $options := map { 'format': 'direct', > 'escape': 'yes' } > > let $j := > json:parse('{"roles":["poet","cleric"],"institutions":["St John's > College","Cambridge > University"],"characterId":"595051204300","homes":["Cheapside","London > ","Dean > Prior","Devon"],"image":"robert_herrick.jpg","DOB":"1591-08-24","attri > butes":["People educated at Merchant Taylors' > School","Northwood","Alumni of St John's College","Cambridge","Alumni > of Trinity Hall","Cambridge","English poets","People from the City of > London","People educated at Westminster School","London","English male > poets"],"DOD":"1674-10-08","nationality":"British","givenName":"Robert > ","familyName":"Herrick"}', > $options) > > return db:add($database, $j, > 'characters/c_595051204300_Robert_Herrick.xml') > > > > Stopped at ., 3/78: > > [XPST0003] Expecting closing bracket: json:parse. at > /usr/local/lib/basex-api/src/main/perl/BaseXClient.pm line 58. > > > > Many thanks, > > Thomas > > >
Written for BaseX 8.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery