1

We have a device with an API that returns JSON strings. I want to be able to access specific portions of the JSON but I can't figure out how to make the syntax work.

use strict;
use warnings;
use CGI qw(:standard);
use LWP::Simple;
use JSON qw(decode_json);
use Data::Dumper;

my $json_request = 'http://<device ip>/API?method=Display:Input:Status:All:Get';
my $raw_json = get $(json_request)
print($raw_json) 
# Output: {"result":{"0":{"status":false},"1":{"status":false},"2":{"status":false},"3":{"status":false},"4":{"type":"unknown","status":true}},"jsonrpc":"2.0"} 

I've tried using the JSON library's decode_json function along with Data::Dumper, but here's what I get:

print(Dumper(decode_json($raw_json)));
# Output: $VAR1 = { 'jsonrpc' => '2.0', 'result' => { '4' => { 'status' => bless( do{\(my $o = 1)}, 'JSON::PP::Boolean' ), 'type' => 'unknown' }, '1' => { 'status' => bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ) }, '3' => { 'status' => $VAR1->{'result'}{'1'}{'status'} }, '0' => { 'status' => $VAR1->{'result'}{'1'}{'status'} }, '2' => { 'status' => $VAR1->{'result'}{'1'}{'status'} } } }; 

Not only does this seem to be even more garbled and it doesn't process some of the elements properly, I can't seem to access elements of either that Dumper output or the raw_json variable. I'm admittedly new to Perl but I've tried all of the syntax I can think of and I can't figure it out.

I would like to be able to just have a table with rows 0 through 4 that just says "true" or "false" depending on the entry for that particular JSON element. E.G. for the JSON result here,

0: False
1: False
2: False
3: False
4: True
5
  • 1
    "Not only does this seem to be even more garbled and it doesn't process some of the elements properly" — It looks fine to me. What did you expect? Perl doesn't have a native boolean type, so you get JSON::PP::Boolean objects instead … and object property order is not guaranteed in JSON nor is hash key property order guaranteed in Perl. Commented Dec 5, 2018 at 15:42
  • "I can't seem to access elements of either that Dumper output or the raw_json variable" — Well, naturally. Both of those are strings. You need to use the output of decode_json() Commented Dec 5, 2018 at 15:43
  • Quentin okay, so how would I do that? Commented Dec 5, 2018 at 15:49
  • perlmaven.com/perl-hash Commented Dec 5, 2018 at 15:51
  • cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash Commented Dec 5, 2018 at 15:51

1 Answer 1

7

I'm not sure what you mean, as far as I can see your JSON contains all the data you want to extract:

$ echo '{"result":{"0":{"status":false},"1":{"status":false},"2":{"status":false},"3":{"status":false},"4":{"type":"unknown","status":true}},"jsonrpc":"2.0"}' | json_pp 
{
   "jsonrpc" : "2.0",
   "result" : {
      "0" : {
         "status" : false
      },
      "4" : {
         "type" : "unknown",
         "status" : true
      },
      "1" : {
         "status" : false
      },
      "3" : {
         "status" : false
      },
      "2" : {
         "status" : false
      }
   }
}

Maybe you have not understood how to access it correctly? The following code seems to work:

#!/usr/bin/perl
use strict;
use warnings;

use JSON qw(decode_json);

my $raw_json = '{"result":{"0":{"status":false},"1":{"status":false},"2":{"status":false},"3":{"status":false},"4":{"type":"unknown","status":true}},"jsonrpc":"2.0"}';
print "${raw_json}\n";

my $object = decode_json($raw_json)
    or die "JSON parse error\n";

my $result = $object->{result};
die "No result object found\n"
    unless $result;

foreach my $key (sort keys %{$result}) {
    print "${key}: ", $result->{$key}->{status} ? "True": "False", "\n";
}

exit 0;

Output:

{"result":{"0":{"status":false},"1":{"status":false},"2":{"status":false},"3":{"status":false},"4":{"type":"unknown","status":true}},"jsonrpc":"2.0"}
0: False
1: False
2: False
3: False
4: True
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.