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

parse json data with underscore symbol

by amaa11 (Initiate)
on Aug 18, 2018 at 06:57 UTC ( [id://1220560]=perlquestion: print w/replies, xml ) Need Help??

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

Dear all,
when I run the below code,I got this error
Not a HASH reference at mojo.pl line 112.
which refer the last line when I print it
How to fix it please??
I want to print the sample size (3,117) and other attributes
Thanks in advance
=========================
use strict; use warnings; use JSON qw( decode_json ); my $json = '{ "_embded" : { "stes" : [ { "SampleSize" : "3,117 ", "ge" : false, "gx" : false, "Count" : 1922309, "qualifier" : null, "imputed" : true, "pooled" : false, "Comment" : null, "Id" : "G00854", "full" : false, "Requested" : false, "forms" : [ { "manufacturer" : "ymetrix" } ], "ries" : [ { "te" : "initial", "Individuals" : 4390, "Groups" : [ { "Group" : "European" } ], "Origin" : [ ], "Recruitment" : [ { "majorArea" : "Europe", "region" : "Northern Europe", "countryName" : "U.K." } ] }, { "te" : "epion", "Individuals" : 2698, "Groups" : [ { "Group" : "European" } ],<br> "country" : [ ], "Recruitment" : [ { "majorArea" : "Europe", "region" : "Western Europe", "countryName" : "Germany" } ] }, { "te" : "ation", "Individuals" : 1649, "Groups" : [ { "Group" : "NR" } ], "country" : [ ], "Recruitment" : [ ] } ], "sea" : { "trt" : "risk" }, "Technologies" : [ { "Technology" : "GW" } ], "SampleSize" : "2,698 ", "publicationInfo" : { "Id" : "247", "publicationDate" : "2016-12-01", "publication" : "Psychiat", "title" : "GTW.", "author" : { "fullname" : "Perlis RH", "orcid" : null }, "_links" : { "stes" : { "href" : " ", "teated" : true } } }, "_links" : { "self" : { "href" : "" }, "sd" : { "href" : "", "templated" : true }, "asmary" : { "href" : "" }, "ets" : { "href" : "" }, "ss" : { "href" : "" }, "aons" : { "href" : "" } } } ] }, "_links" : { "self" : { "href" : "" } } }'; my $decoded = decode_json($json); # This is a Perl example of parsing a JSON object. print "SampleSize = " . $decoded->{'_embded'}{'stes'}{'SampleSize'} . +"\n";

Replies are listed 'Best First'.
Re: parse json data with underscore symbol
by choroba (Cardinal) on Aug 18, 2018 at 07:15 UTC
    Please, fix your question using the <code>...</code> tags.

    "stes" seems to be an array, so you need to index it with square brackets:

    print 'SampleSize = ' . $decoded->{_embded}{stes}[0]{SampleSize}, "\n" +;

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      When the code and JSON are formatted properly (OP updated their post a few times), your suggestion fixes the problem.
      Thanks a lot, it does print correctly now.
      But when i want print ge value
      print 'ge= ' . $decoded->{_embded}{stes}[1]{ge}, "\n"
      I got this error.
      Use of uninitialized value in concatenation (.) or string

        Array indices are zero-based. The "ge" key is in the first element (i.e. index 0). So:

        $decoded->{_embded}{stes}[1]{ge} # BAD $decoded->{_embded}{stes}[0]{ge} # GOOD $decoded->{_embded}{ries}[1]{te} # BAD $decoded->{_embded}{ries}[0]{te} # GOOD

        By the way, "Use of uninitialized value ..." is a warning, not an error. See "perldiag - Perl diagnostic messages" for a description of this warning.

        — Ken

Re: parse json data with underscore symbol
by kevbot (Vicar) on Aug 18, 2018 at 07:16 UTC
    Hello amaa11, Please see Writeup Formatting Tips for advice on how to properly format your post. When I copy & paste your code from the perlmonks page and run it, I get a error related to improperly formatted JSON. I think your JSON string is not being displayed properly on the site, and this should be fixed by wrapping your code with <code> tags.
      Hello amaa11,

      I see that you added the code tags, but now your code is no longer valid perl syntax due to the appearance of extra <br> tags on every line.

      UPDATE: I see that you fixed the formatting problems, but now when I run your code I get an error related to improperly formatted JSON. When I run your code as is, I get this error '"' expected, at character offset 811 (before "<br>\n        "count...") at t.pl line 108. When I manually remove the offending <br> tag, I finally get the error that you mentioned Not a HASH reference at t.pl line 110.
        Dear kevbot,
        Yes I updated the all post as you asked
        still have a small problem when I print the second element in the array
        print 'ge= ' . $decoded->{_embded}{stes}[1]{ge}, "\n"
        I got this error.
        Use of uninitialized value in concatenation (.) or string

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-26 05:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found