Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Converting hash into js object properties

by nysus (Parson)
on Nov 21, 2017 at 15:52 UTC ( [id://1203900]=perlquestion: print w/replies, xml ) Need Help??

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

What module exists where I can take a hash like this:

my %hash = ( rules => {name => '"required"', email => { required => true, email => true } }, message => { name => '"Please specify your name"', email => { required => '"We need your email address to +contact you"', email => '"Your email address must be in the format +of name@domain.com"', } } );
</code>

And convert it to something like this for use in a js function:

rules: { name: "required", email: { required: true, email: true } }, messages: { name: "Please specify your name", email: { required: "We need your email address to contact you", email: "Your email address must be in the format of name@domain. +com" } }

Ideally, the module would be smart enough to know when to double quote the values and when not to.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Converting hash into js object properties
by haukex (Archbishop) on Nov 21, 2017 at 15:53 UTC

      Just tried that but the property keys get surrounded by quotes in the JSON output. I'm not sure if that will make a difference.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

        nysus:

        It will make a difference, but in a good way, as the JSON format wants keys to be wrapped in quotes: The JSON spec indicates that the object keys are strings, and that strings are wrapped in double quotes. So you should be good to go with the module.

        I personally use JSON, which as I understand it, tries to use the first of JSON::XS, JSON::PP or JSON::backportPP that it finds on your machine.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        my %hash = ( rules => {name => '"required"', ...

        Just tried that but the property keys get surrounded by quotes in the JSON output.

        The module is just faithfully converting your Perl data structure into a JSON one. Your Perl strings contain the double quotes, so the JSON ones do too. If you don't want that, strip the double quotes from the Perl strings before converting to JSON (or don't add them in the first place).

        Update: Sorry, I thought you were talking about the extra quotes in the values, but you meant the keys. See replies below.

      A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1203900]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-18 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found