bigup401 has asked for the wisdom of the Perl Monks concerning the following question:

use JSON qw(to_json); $fname = "John"; my %rec_hash = (firstname => $fname); my $json = to_json \%rec_hash; print "$json\n";

this is json reponse

{"firstname":"John"}

i want to remove double quote on John

and have response {"firstname":John}

Replies are listed 'Best First'.
Re: quote in json
by marto (Cardinal) on May 08, 2015 at 14:56 UTC
    {"firstname":John}

    Is not valid JSON. If you want to return something that isn't valid JSON, don't use the JSON module.

    Update: link to jsonlint.com.

Re: quote in json
by edimusrex (Monk) on May 08, 2015 at 14:55 UTC
    Try something like this (probably a better way to write the regex)

    use JSON qw(to_json); $fname = "John"; my %rec_hash = (firstname => $fname); my $json = to_json \%rec_hash; $json =~ s/(.+?\:)\"(\w+)\"/$1$2/g; print "$json\n";
Re: quote in json
by QM (Parson) on May 11, 2015 at 10:54 UTC
    Also note that some modules (in various languages) accept a superset of JSON, such that one may accept unquoted strings as values, while another will not.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      But, “some languages will accept ...” of course, is never the way that one should go.   JSON modules, in various languages, were designed to be universal – and, above all, safe.   (JSON started out, in the younger, more naïve days of the Internet, as, “let’s just eval the string!   ‘Efficient,’ huh?”)   Heh.   You can afford the couple extra bytes, on whatever platforms and languages you might be using, to use a supplied, standard package to do the work for you.   (Especially on the wild-and-wooly JavaScript side!)