Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Escape $ in JSON::XS decoded

by johnfl68 (Scribe)
on Oct 16, 2020 at 20:27 UTC ( #11122920=perlquestion: print w/replies, xml ) Need Help??

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

Hello, is there a any other way to escape the $ as a decoded JSON character?

Google in their infinite wisdom decided to use the $ as part of value names:

(edited)

"updated":{ "$t":"2019-07-19T20:04:56.046Z"

But DataDumper shows the current time:

$VAR1 = { 'version' => '1.0', 'feed' => { 'xmlns' => 'http://www.w3.org/2005/Atom', 'updated' => { '$t' => '2020-10-16T19:55:33.294Z +' }, 'gs$rowCount' => { '$t' => '1000'

I am trying to get the Feed Updated value, this gives me the current time, and not the time value of 2019-07-19 in this case:

$updated    = $decoded_json->{feed}->{updated}->{'$t'};

This:

$updated    = $decoded_json->{feed}->{updated}->{'\$t'};

Gives me: Use of uninitialized value $updated in concatenation (.) or string

Normally I just escape the special character, but it seems to be interpreting it into something different, and I am not sure why? I don't usually have characters in JSON that need to be escaped like this.

Everything else is working fine, except this one line. Am I missing something that I just haven't stumbled upon before? Any help would be appreciated as always.

Thank you.

Replies are listed 'Best First'.
Re: Escape $ in JSON::XS decoded
by choroba (Archbishop) on Oct 16, 2020 at 20:36 UTC
    I can't reproduce your problem. There's no need to escape dollars in single quoted strings.
    #!/usr/bin/perl use strict; use warnings; my $decoded_json = { 'version' => '1.0', 'feed' => { 'xmlns' => 'http://www.w3.org/2005/Atom', 'updated' => { '$t' => '2020-10-16T19:55:33.294Z' }, 'gs$rowCount' => { '$t' => '1000' }}}; print $decoded_json->{feed}->{updated}->{'$t'}; # 2020-10-16T19:55:33 +.294Z

    How should the code return 2019-07-19 when the structure contains 2020-10-16? Maybe the problems happens in the code you didn't show - how do you create the JSON?

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      My apologies, I didn't catch that, but it explains part of the problem.

      In the actual JSON file, it reads as follows:

      "updated":{ "$t":"2019-07-19T20:04:56.046Z"

      But when Perl and or JSON::XS is loading the JSON file, it seems to be reinterpreting the $t some how as the current time. I have not declared $t at all.

      I knew what the answer was supposed to be in my head, and my brain just didn't connect that the DataDummper was putting the current time, as I was looking at the raw JSON.

      This just baffles me even more. I'll keep digging.

        I can't reproduce that either. It still returns the value stored in the original JSON, as expected.
        #!/usr/bin/perl use strict; use warnings; use JSON::XS; my $json = '{"feed":{"updated":{"$t":"2019-07-19T20:04:56.046Z"}}}'; my $decoded_json = decode_json($json); print $decoded_json->{feed}->{updated}->{'$t'}; # 2019-07-19T20:04:56 +.046Z
        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        But when Perl and or JSON::XS is loading the JSON file, it seems to be reinterpreting the $t some how as the current time.

        Sorry, but almost certainly not.

        use warnings; use strict; use Data::Dump; use JSON::XS; dd decode_json('{ "updated":{ "$t":"2019-07-19T20:04:56.046Z" } }') __END__ { updated => { "\$t" => "2019-07-19T20:04:56.046Z" } }

        Again, please provide a Short, Self-Contained, Correct Example that we can run that shows otherwise.

Re: Escape $ in JSON::XS decoded
by tobyink (Canon) on Oct 16, 2020 at 22:26 UTC

    I've not had any problems with JSON that has dollar signs in keys in any of the common Perl JSON implementations; such things are pretty common, like in the JSON Schema spec.

    Pretty sure you're doing something weird and the issue isn't in the JSON modules. Please post full code of something that behaves unexpectedly.

    This thing which you posted should work:

    $decoded_json->{feed}->{updated}->{'$t'};
Re: Escape $ in JSON::XS decoded
by haukex (Bishop) on Oct 16, 2020 at 20:38 UTC

    The string as you've shown in your (I assume) Data::Dumper output is the two characters $ and t, and {'$t'} is the correct way to access the hash entry whose key is that two-character string. The string '\$t' consists of three characters, \, $ and t, because backslashes in single-quoted strings work differently than in double-quoted strings - see Quote and Quote like Operators. You don't need to escape $ in a single-quoted string.

    I am trying to get the Feed Updated value, this gives me the current time, and not the time value of 2019-07-19

    Sorry, this doesn't make sense to me. Based on what you've shown here, there is no way that accessing the key differently would give you a different value. I suspect the data structure actually contains the value it's showing you. If you continue to have trouble, please provide a Short, Self-Contained, Correct Example that reproduces the isse.

Re: Escape $ in JSON::XS decoded
by jo37 (Friar) on Oct 16, 2020 at 21:36 UTC

    You did not provide the full content neither of $VAR1 nor of the JSON. By chance, is there another "updated" entry besides the one under the "feed" key with the current timestamp and you're just confusing these?

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11122920]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2020-10-31 11:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (288 votes). Check out past polls.

    Notices?