Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Iterate JSON File

by mijares93 (Initiate)
on Aug 02, 2018 at 16:36 UTC ( #1219718=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, I am new to Perl and i want to parse a JSON File, but right now i am stuck.

Right now i am able to extract data from "observations"->"op_path" which results in: "Lib::Execution", but i want to access to the "status" and extract "kind", "path", "info"

The way i am extracting the data from "observations"->"op_path" is like this

my $decoded = decode_json($json); my $aref = $decoded->{"observations"}; foreach my $element (@$aref){ $final .= $element->{"op_path"}.","; } print $final;

The json file is the following one:

{ "generation_date": "", "static_info": "", "justification_files": [], "test_procedure_files": [ "" ], "observations": [ { "op_path": "Lib::Execution", "c_op_name": "Lib", "mono_info": "", "status": [ { "kind": "I", "path": "_L7=", "info": "%(MinorFrameCnt)%", "stat": "O" }, { "kind": "I", "path": "_L2=", "info": "%(Offset)%", "stat": "O" }, { "kind": "I", "path": "_L1=", "info": "%(Period)%", "stat": "O" }, { "kind": "I", "path": "_L6=-1", "info": "%(_L3)% = _L8", "stat": "O" }, { "kind": "I", "path": "_L6=-2", "info": "_L3 = %(_L8)%", "stat": "O" } ] }

Hope that you guys could help me with this issue

Replies are listed 'Best First'.
Re: Iterate JSON File
by VinsWorldcom (Prior) on Aug 02, 2018 at 16:52 UTC

    Probably not the best way, but this works:

    #!perl use strict; use warnings; use JSON::PP; my $json; { local $/ = undef; open my $fh, '<', 'in.json'; $json = <$fh>; close $fh; } my $final; my $decoded = decode_json($json); for my $ob ( @{$decoded->{observations}} ) { for my $status ( @{$ob->{status}} ) { for my $elem ( sort ( keys ( %{$status} ) ) ) { print $elem . "=" . $status->{$elem} . "\n"; } print "\n"; } }

    and outputs ...

    info=%(MinorFrameCnt)% kind=I path=_L7= stat=O info=%(Offset)% kind=I path=_L2= stat=O info=%(Period)% kind=I path=_L1= stat=O info=%(_L3)% = _L8 kind=I path=_L6=-1 stat=O info=_L3 = %(_L8)% kind=I path=_L6=-2 stat=O

      Thank you for you answer, actually it worked great

      But now i want to only search for the "kind" tag and here is what i did:

      for my $ob ( @{$decoded->{observations}} ) { for my $status ( @{$ob->{status}} ) { for my $kind (@{$status->{info}}){ for $elem ( sort ( keys ( %{$kind} ) ) ) { $final1 .= $kind->{$elem} .","; } } } }

      I am getting an error that says that Cant use string (...) as an ARRAY ref while "stricts refs" in use at...

      I think that is something related to deference, but i am not sure how to solve it, could you help me?

      Regards

        You seem to think that  $kind is a hash reference, but it isn't; it's a string, and strings (a.k.a. "symbolic references") can't be dereferenced under strict 'refs'. It's important to know the nature of the data you're dealing with. To visualize the type and value (update: and structure!) of data, use a module like Data::Dumper (which is core, so it should already be installed), or Data::Dump (which I like more, but which is not core). So maybe something like

        use Data::Dumper; ... for my $kind (@{$status->{info}}){ print Dumper $kind; ... }
        to get started. Once you know where you are, it will be easier to see the way forward.


        Give a man a fish:  <%-{-{-{-<

Re: Iterate JSON File
by kcott (Bishop) on Aug 06, 2018 at 06:45 UTC

    G'day mijares93,

    Welcome to the Monastery.

    [Sorry, I'm a little late to the party on this one: I'm catching up after some travelling.]

    The data returned from decode_json is typically a complex data structure and you can access its internal parts quite easily. Consider this short script:

    #!/usr/bin/env perl use strict; use warnings; use autodie; use JSON; my $json_file = 'pm_1219718_data.json'; my $json_text = do { open my $fh, '<', $json_file; local $/; <$fh> }; my $perl_data = decode_json $json_text; for (@{$perl_data->{observations}[0]{status}}) { print "$_->{path}\n" }

    Output:

    _L7= _L2= _L1= _L6=-1 _L6=-2

    The "$json_file" is what you posted plus

    ] }

    to close the "observations" array (with the ']') and the JSON object as a whole (with the '}').

    "... I am new to Perl ..."

    Here's some suggested reading:

    — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1219718]
Approved by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2021-02-25 18:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?