Home > Enterprise >  Perl and JSON data with quotes
Perl and JSON data with quotes

Time:11-02

I am using this Perl module JSON::XS to convert a Hash to JSON , where i am creating the hash on the fly and converting it to json using the below :

print  encode_json \%hash;

the JSON is converted to something like this :

{
    "info": ["test","test2"],
    "name": "test",
    "uid": "1"
}

is it possible to convert it to something like this :

{
        info: ['test','test2'],
        name: 'test',
        uid: '1'

}

i.e removing the ' from the keys and replacing "" with '' on the values ?any idea how to achieve this?

CodePudding user response:

That is not valid JSON. Keys must be quoted string literals, and only double-quotes can be used to quote string literals. You will not be able to use a JSON serializer to do this. You will need to write your own serializer for your own language.

CodePudding user response:

$_ = <<'EOL'
{
    "info": ["test","test2"],
    "name": "test",
    "uid": "1"
}
EOL
;
$_ =~ s/"/'/g;
say $_;
  • Related