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

{[ {'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::PP_SUPPORT_METHODS
$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


In reply to Escaping quotes in JSON string by HeadScratcher

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.