HeadScratcher has asked for the wisdom of the Perl Monks concerning the following question:
Hi
I have some JSON data in a file that I need to process how do I escape single and double quotes
the sample here demonstrates what I'm up against
JSON::PP_SUPPORT_METHODS{[ {'firstName':'John', 'lastName':'Doe', 'text' : 'Text with new lines and tabs' }, {'firstName':'Anna', 'lastName':'Smith', 'text' : 'text with ''2 single quotes togeether '' and "Double +quotes either in pairs" or in ""Double pairs"" ' }, {'firstName':'Peter', 'lastName':'O'Toole', 'text' : "More text with diacritics' ]}
$json->allow_singlequote->decode({'foo':'bar'});
I think will fix the single quote problem in the 'key' : 'value' pair however how do I deal with quotes single & double within the text? I'm getting errors like ", or expected while parsing object/hash at charecter offest 1234 (before "'2 single quotes'' .....")
use strict; use warnings; use JSON -support_by_pp; my $content = qq( {[ {'firstNameA' : 'John', 'lastName' : 'Doe', 'text' : 'Text with new lines and tabs' }, {'firstNameB' : 'Anna', 'lastName' : 'Smith', 'text' : 'text with ''2 single quotes togeether '' and "Doubl +e quotes either in pairs" or in ""Double pairs"" ' }, {'firstNameC' : 'Peter', 'lastName' : 'O'Toole', 'text' : "More text with diacritics' ]} ); print $content; my $json = new JSON; my $json_text = $json->allow_nonref->utf8->relaxed->escape_slash-> +loose->allow_singlequote->decode($content);
BTW the sample data here is just for show the actual file can not be uploaded
Thank You
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Escaping quotes in JSON string
by RichardK (Parson) on Oct 11, 2015 at 13:58 UTC | |
|
Re: Escaping quotes in JSON string
by AnomalousMonk (Archbishop) on Oct 11, 2015 at 18:17 UTC | |
|
Re: Escaping quotes in JSON string
by ikegami (Patriarch) on Oct 12, 2015 at 01:04 UTC | |
|
Re: Escaping quotes in JSON string
by HeadScratcher (Novice) on Oct 11, 2015 at 17:14 UTC | |
by Corion (Patriarch) on Oct 11, 2015 at 19:01 UTC | |
|
Re: Escaping quotes in JSON string
by nikosv (Deacon) on Oct 12, 2015 at 10:40 UTC |