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

Hey Monks! I'm new here and I would like to ask you for help. So I've got a JSON File, may it be something like this:
{ "local": { "tomcat": { "general": { "catalina_home": "somewhere/tomcat/current", "install_dir": "somewhere/tomcat/8.5.20", "name": "Tomcat Web Application Server", "version": "1.8u131" } "instances" { "instance_name": { "catalina_base": "somewhere/tomcat_instances/instan +ce_name", "port" { "http": 18081, "https": 19081 }, "unix_group": "tmcuser", "unix_user": "tomcat1" }, "instance_name2": { "catalina_base": "somewhere/tomcat_instances/instan +ce_name2", "port" { "http": 18082, "https": 19082 }, "unix_group": "tmcuser", "unix_user": "tomcat2", } } } } }
I want to read out certain information out of that JSON File and I already found out how to read informations which contains data (f.e. "catalina_base"), but I also would like to read out the instance names ("instance_name" and "instance_name2"). The way I read out the informations containing data is by for example:
(...) my $data = decode_json($json); my $Catalina_Base = $data->{'local'}->{'tomcat'}->{'instances'}->{' +instance_name'}->{'catalina_base'}; (...)
How am I able to read out the instance name? If I try
(...) my $data = decode_json($json); my $Instance_Name = $data->{'local'}->{'tomcat'}->{'instances'}->{' +instance_name'}; (...)
I get every single information instance_name contains like catalina_base, port etc. but I just want the name of the instance. Is there any way to do that? I would be really thankful if you could help me. Greetings Furkan

Replies are listed 'Best First'.
Re: reading special parts of a JSON File
by choroba (Cardinal) on Sep 18, 2017 at 09:29 UTC
    Use keys to get a list of keys. I had to fix the input JSON in several places (missing colons, trailing commas).
    #! /usr/bin/perl use warnings; use strict; use feature qw{ say }; use JSON::XS; my $json = '{ "local": { "tomcat": { "general": { "catalina_home": "somewhere/tomcat/current", "install_dir": "somewhere/tomcat/8.5.20", "name": "Tomcat Web Application Server", "version": "1.8u131" }, "instances": { "instance_name": { "catalina_base": "somewhere/tomcat_instances/instan +ce_name", "port": { "http": 18081, "https": 19081 }, "unix_group": "tmcuser", "unix_user": "tomcat1" }, "instance_name2": { "catalina_base": "somewhere/tomcat_instances/instan +ce_name2", "port": { "http": 18082, "https": 19082 }, "unix_group": "tmcuser", "unix_user": "tomcat2" } } } } }'; my $data = decode_json($json); say for keys %{ $data->{local}{tomcat}{instances} };
    ($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,
Re: reading special parts of a JSON File
by Eily (Monsignor) on Sep 18, 2017 at 09:26 UTC

    Your question is a bit confusing, but from what I understand you want to use keys:

    my $instances_hash = $data->{'local'}->{'tomcat'}->{'instances'}; my @names = keys %$instances_hash; for my $name (@names) { print "The name is $name\n"; }

      Thank you very much! Thanks to you I figured out how to solve my problem, thanks a lot!
      I'm sorry that my question is confusing. I want to get the instance name (which is in "instance_name") and safe it into a variable for further uses. I'll try your idea, thank you!