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

how can i read string into json output

$name = 'mike'; $age = "20"; $email = "johndeo@/test.com"; $pass = "test"; $resp = '{ "$name": $age, "$email": $pass }'; print $resp;

Replies are listed 'Best First'.
Re: strings to json
by tobyink (Canon) on Jun 23, 2017 at 13:42 UTC

    This will work:

    use strict; use warnings; my $name = 'mike'; my $age = "20"; my $email = "johndeo\@test.com"; my $pass = "test"; my $resp = "{ \"$name\": $age, \"$email\": \"$pass\" }"; print $resp;

    But escaping is likely to be annoying. I'd personally do something more like this:

    use strict; use warnings; use JSON::PP; my $name = 'mike'; my $age = "20"; my $email = "johndeo\@test.com"; my $pass = "test"; my $resp = JSON::PP->new->encode({ $name => $age, $email => $pass, }); print $resp;

      The first solution is broken; it suffers from JSON-injection bugs. For example, try

      my $name = 'Mike "The Coder" Smith';

      or

      my $pass = 'S3KYU\\3e';

        It's only broken if you change the other code so $name, etc are different.

        But if you change the surrounding code, it's possible to break anything.

      thanks guys

Re: strings to json
by Corion (Patriarch) on Jun 23, 2017 at 13:44 UTC

    It's best to use one of the JSON modules. For example JSON::Tiny, but also JSON::XS or JSON or JSON::Any.

    use strict; use JSON::Tiny 'encode_json'; my $name = 'mike'; my $age = "20"; my $email = "johndeo@/test.com"; my $pass = "test"; my $resp = { $name => $age, $email => $pass }; print encode_json( $resp );

    Your JSON strikes me as weird. I would expect a different JSON structure:

    my $resp = { name => $name, age => $age, email => $email, pass => $pass, };
Re: strings to json
by Eily (Monsignor) on Jun 23, 2017 at 13:42 UTC

    What's your expected output? Because here it kind of looks like you want:

    { "mike":20, "johndeo@/test.com":test }
    which doesn't make much sense.

    One easy way to make json structure in perl, is to simple encore perl structures using JSON:

    use JSON; my %data = (name => "Mike", age => 20, email => "mike@perlmonks.org", +pass => "IAmGroot"); print encode_json(\%data); #edited thanks to marto __DATA__ {"age":20,"pass":"IAmGroot","email":"mike.org","name":"Mike"}

    NB: be careful with double quotes, @ should be interpreted as the sigil of an array variable, even though here it is not for some reason. Simple quotes would be better.

    Edit: the fact that @/ (a punctuation variable) is not interpolated is actually documented in perlop

    "Punctuation" arrays such as @* are usually interpolated only if the name is enclosed in braces @{*}, but the arrays @_ , @+ , and @- are interpolated even without braces.

      encore_json should be encode_json.

Re: strings to json
by marto (Cardinal) on Jun 23, 2017 at 13:53 UTC

    Perhaps it's time to take a step back and review the questions you've posted previously about working with JSON.

Re: strings to json
by roboticus (Chancellor) on Jun 24, 2017 at 12:55 UTC

    bigup401:

    As the other respondants have mentioned, it would be a good idea to use JSON to deal with your JSON data.

    Why a module to handle such a simple format? Primarily because many "simple formats" are only simple on the surface, and have some subtleties that can kick you in the pants when you least expect it. Even when the format is relatively simple, they may be ill-defined and have ambiguous cases. Even worse, if you're exchanging data with other applications that hand-roll the 'simple format', you often find that they have different bugs in their implementation than you do.

    You'd be surprised at how often you'll encounter different variations of XML files that aren't truly XML compliant. You may also be surprised at how many different variations of CSV files there are. Since those problems have already largely been solved by generous contributors to CPAN, you should save yourself some grief and just use JSON;. ;^)

    ...roboticus

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