Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^6: parse json data with underscore symbol

by kevbot (Vicar)
on Aug 20, 2018 at 04:41 UTC ( [id://1220670]=note: print w/replies, xml ) Need Help??


in reply to Re^5: parse json data with underscore symbol
in thread parse json data with underscore symbol

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found