9

i have a json in a file sample.txt

i want to decode whole json and print key values by specifying. my per code is

#!/usr/bin/perl
use JSON; 
use Data::Dumper; 
use JSON::XS qw( decode_json );

open (han1, "sample.txt") or die "can not read this file ";
@array1 = <han1>; 
$tst =  $array1[0];
$text = decode_json $tst; 
print Dumper($text);

I have a key in json name 'messages_ready' . i want to print value of 'messages_ready'..

my json is follows

[
    {
        "arguments": {},
        "auto_delete": false,
        "backing_queue_status": {
            "avg_ack_egress_rate": 55.02128728993393,
            "avg_ack_ingress_rate": 55.02128728993393,
            "avg_egress_rate": 55.02128728993393,
            "avg_ingress_rate": 109.64602476156203,
            "delta": [
                "delta",
                0,
                0,
                0
            ],
            "len": 6465,
            "next_seq_id": 7847104,
            "pending_acks": 4,
            "persistent_count": 0,
            "q1": 0,
            "q2": 0,
            "q3": 0,
            "q4": 6465,
            "ram_ack_count": 4,
            "ram_msg_count": 6465,
            "target_ram_count": "infinity"
        },
        "consumers": 4,
        "durable": true,
        "exclusive_consumer_tag": "",
        "memory": 19373224,
        "message_stats": {
            "ack": 7840491,
            "ack_details": {
                "rate": 60.4
            },
            "deliver": 7840497,
            "deliver_details": {
                "rate": 60.4
            },
            "deliver_get": 7840498,
            "deliver_get_details": {
                "rate": 60.4
            },
            "get": 1,
            "get_details": {
                "rate": 0.0
            },
            "publish": 7847260,
            "publish_details": {
                "rate": 105.4
            },
            "redeliver": 3,
            "redeliver_details": {
                "rate": 0.0
            }
        },
        "messages": 6469,
        "messages_details": {
            "rate": 74.6
        },
        "messages_ready": 6465,
        "messages_ready_details": {
            "rate": 74.6
        },
        "messages_unacknowledged": 4,
        "messages_unacknowledged_details": {
            "rate": 0.0
        },
        "name": "reports",
        "node": "rabbit@ip-10-0-0-105",
        "policy": "",
        "status": "running",
        "vhost": "/"
    },
    {
        "arguments": {},
        "auto_delete": false,
        "backing_queue_status": {
            "avg_ack_egress_rate": 0.0,
            "avg_ack_ingress_rate": 0.0,
            "avg_egress_rate": 0.0,
            "avg_ingress_rate": 0.0,
            "delta": [
                "delta",
                "undefined",
                0,
                "undefined"
            ],
            "len": 1,
            "next_seq_id": 1,
            "pending_acks": 0,
            "persistent_count": 0,
            "q1": 0,
            "q2": 0,
            "q3": 0,
            "q4": 1,
            "ram_ack_count": 0,
            "ram_msg_count": 1,
            "target_ram_count": "infinity"
        },
        "consumers": 0,
        "durable": true,
        "exclusive_consumer_tag": "",
        "idle_since": "2013-12-31 13:03:35",
        "memory": 13760,
        "message_stats": {
            "publish": 1,
            "publish_details": {
                "rate": 0.0
            }
        },
        "messages": 1,
        "messages_details": {
            "rate": 0.0
        },
        "messages_ready": 1,
        "messages_ready_details": {
            "rate": 0.0
        },
        "messages_unacknowledged": 0,
        "messages_unacknowledged_details": {
            "rate": 0.0
        },
        "name": "test",
        "node": "rabbit@ip-10-0-0-105",
        "policy": "",
        "status": "running",
        "vhost": "/"
    }
]

How would i do this ? help me here...please

1
  • I suggest you use JSON::Tiny if you aren't using everything JSON::XS is offering, JSON::Tiny as the name says, has a tiny overhead and works a lot better if performance is important to you Commented Jan 1, 2014 at 13:52

2 Answers 2

10
  • decode_json() returns a reference to an array or hash depending on the data. In this case it's a reference to an array of hash references
  • The foreach loop uses @$json_data to access the array elements in $json_data, assigning each in turn to $section. $section is now a hash reference.
  • Use $section->{some-key} to access the keys, as in $section->{'messages_ready'}
  • Always remember to use strict

Something like this:

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

open (han1, "sample.txt") or die "can not read this file: $!\n";
my $json_string = join '', <han1>;
my $json_data = decode_json $json_string;

foreach my $section (@$json_data) {
    print "messages_ready: " . $section->{'messages_ready'} . "\n";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Don't be giving me ideas now Kenosis... ;)
9

Note that you don't need to use JSON and JSON::XS in one script: JSON will automatically use JSON::XS if will find it. So use JSON is the same, as use JSON::XS, but more portable.

use strict;
use warnings;
use JSON::XS 'decode_json';
use Data::Dumper;

my $data;
{
    local $/ = undef;
    open my $fh, '<', 'metadata.txt';
    $data = <$fh>;
    close $fh;
}

my $result = decode_json( $data );

for my $report ( @{$result} ) {
    print $report->{messages_ready}, "\n";
}

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.