Perl - Pass JSON data to variable and compare to IF

0

Good morning,

I have a request that I make in an API using GET (LWP :: UserAgent), the data is returned in a JSON, the JSON are returned up to two results at most as follows:

{"status":1,"time":1507891855,"response":{"prices":{"nome1\u2122":
{"preco1":1111,"preco2":1585,"preco3":1099},"nome2":
{"preco1":519,"preco2":731,"preco3":491}}}}

Dump:

$VAR1 = {
      'status' => 1,
      'time' => 1507891855,
      'response' => {
                      'prices' => {
                                    'nome1' => {
                                                 'preco1' => 1111,
                                                 'preco3' => 1099,
                                                 'preco2' => 1585
                                               },
                                    'nome2' => {
                                                 'preco3' => 491,
                                                 'preco1' => 519,
                                                 'preco2' => 731
                                               }
                                  }
                    }
    };

What I would like to do is:

Take this data and save it in a variable to make a comparison using IF with another variable that already has the name stored, the comparison would be with name1 / name2 and if it is true with the other variable it would get preco2 and preco3 to start everything.

My biggest problem in the case is that some of these names in JSON contain characters like (TradeMark) that come as \ u2122 (some cases are other characters), so I can not make the comparison with the name of the other variable that is already with the correct name

nome1™

If I could just save the JSON already "converted" the characters would help me turn around with the rest.

Basically after doing the request for the API I want to save the contents in a variable already converting all \ u2122 to their respective character (this is the part that I do not know how to do in Perl) and then using another variable compare if the names are the same to show the price.

Thank you in advance for the help and any questions please tell me that I try to explain it again in another way.

    
asked by anonymous 13.10.2017 / 12:49

1 answer

0

Problem solved using the following function:

use utf8;
use JSON::XS;

my $name = "nome1™";
my $var1 = decode_json $utf8_encoded_json_text;

# Compare with name in $name
if( defined $var1->{'response'}->{'prices'}->{$name} ) {
# Do something with the name that matches
my $match = $var1->{'response'}->{'prices'}->{$name};

print $match->{'preco1'}, "\n";
}
    
15.10.2017 / 16:16