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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello amaa11,

It's difficult to give advice because the code you need to write will be highly dependent on the data structure returned by decode_json, which is highly dependent on the structure of your JSON string.

If you are going to be extracting data from complex JSON strings, then it may be helpful to use a module that helps you extract the data (without having to worry about the perl data structure). One such module is JSON::Path. It lets your write a JSONPath string to extract the data. This way you can write a JSONPath string that will extract only the portion of data that you are interested in and it will reduce the amount of perl code you need to write. I have converted your script of use JSON::Path to give you an example to work from.

#!/usr/bin/env perl use strict; use warnings; use JSON::Path; 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" } ], "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 $jpath_stes = JSON::Path->new('$._embded.stes[*]'); # @vals will contain all of the elements of 'stes' my @vals = $jpath_stes->values($json); my $jpath_ge = JSON::Path->new('$._embded.stes[0].SampleSize'); # $val will contain the value found for 'SampleSize' in the first elem +ent of # the stes array my $val = $jpath_ge->value($json); print "SampleSize: $val\n"; exit;
If you choose to use this module, I recommend you carefully read the documentation for the JSON::Path and JSONPath - XPath for JSON.

In reply to Re^6: parse json data with underscore symbol by kevbot
in thread parse json data with underscore symbol by amaa11

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-23 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found