Parse Json using Perl module JSON::Parse

Parse Json using Perl module JSON::Parse

Satya Perl Solution , , ,

Parse Json using Perl module JSON::Parse:  Json is Java script Object Notation. It is a text in Java object notation, which is being used to store or exchange data. This helps in transmitting data object, which consist of attribute value pair and array data type. It was initially created for java script but now a days it is being supported by all programming language. Therefore, it is language independent.

In this post, I will explain how we can load and parse json data by using Perl JSON::Parse Module.

If you are new to Perl then refer Perl short tutorial.

About JSON::Prase

This module contains subroutine, which Parse Json and read parsed data into variable.

 

Lets see how we can Parse Json using Perl module JSON::Parse.

To demonstrate json parsing through Perl I have used UCD rest to get Udeploy agent information in json format.

 

Below example shows udclinet CLI execution to get agent information from uDeploy. You can run this from Unix shell. It returns information in json format. This returned string I will use to demonstrate parsing, using Perl module JSON::Prase.

 

$UCD_PATH/udclient -username admin -weburl https://localhost:8445 getAgent -agent myAgent
Password : *****
{
  "id": "9b631e4c-6771-4817-a718-d7c473d31732",
  "securityResourceId": "be689610-a0cc-494f-8f34-09cf05630208",
  "name": "myAgent",
  "active": true,
  "licensed": false,
  "licenseType": "NONE",
  "status": "ONLINE",
  "version": "6.2.2.0.825094",
  "lastContact": 1534235502367,
  "dateCreated": 1527705135984,
  "workingDirectory": "\/apps\/ibm-ucd3\/agent\/var\/work\/",
  "impersonationPassword": "****",
  "impersonationUseSudo": false,
  "impersonationForce": false,
  "tags": [],
  "propSheet": {
     "id": "0b2c81d5-643f-4f52-bb8c-de541d533423",
     "path": "agents\/9b631e4c-6771-4817-a718-d7c473d31732\/propSheet",
     "version": 9,
     "versionCount": 9,
     "commit": 122,
     "versioned": true
  },
  "extendedSecurity": {
     "read": true,
     "Add to Agent Pool": true,
     "Create Resources": true,
     "Delete": true,
     "Edit Basic Settings": true,
     "Manage Impersonation": true,
     "Manage Properties": true,
     "Manage Teams": true,
     "Manage Version Imports": true,
     "View Agents": true,
     "teams": []
  },
  "security": {
     "read": true,
     "Add to Agent Pool": true,
     "Create Resources": true,
     "Delete": true,
     "Edit Basic Settings": true,
     "Manage Impersonation": true,
     "Manage Properties": true,
     "Manage Teams": true,
     "Manage Version Imports": true,
     "View Agents": true
   }
}

Jason::Parse script

To parse this i have used below script.

use JSON::Parse ':all';
use Term::ReadKey;
use Data::Dumper;
use strict;

print ("Enter ucd password:");
ReadMode('noecho'); # don't echo
chop(my $password=<STDIN>);
ReadMode('restore');
print "\n";
my $cmd="/apps/ibm-ucd3/agent/opt/udclient/udclient -username admin -password $password -weburl https://localhost:8445 getAgent -agent myAgent";
my $response=`$cmd`;
my $parsed=parse_json($response);

#print Dumper($parsed);
print "Agent Name: $parsed->{name}\n";
print "Agent id: $parsed->{id}\n";
print "Agent status: $parsed->{status}\n";

 

In the above script, first thing we to do is load Json::Parse module. After that we get password from user input and then we pass all the necessary parameters to the ucd CLI. We run the udeploy cli command to get response from udeploy. UCD CLI (udclient) fetch ucd agent(myAgent) information in json format(as a response). This we assign to a variable. There after, this response in the form of variable passed(as a parameter) to the parse_json() subroutine. This subroutine parse json string and return hash ref ($parsed).

We can use this hash reference to get particular value by referring correct key or index. On execution, above script generate following result.

Agent Name: myAgent
Agent id: 9b631e4c-6771-4817-a718-d7c473d31732
Agent status: ONLINE

 

For more detail on Json::Parse refer cpan.

 

 

 

You May Also Like..

How to remove tags from HTML in Perl

HMost often, in development project, you get requirement to remove tags from HTML page to extract text. Different programming languages […]

How concatenate string in Perl

How to concatenate string in Perl

How to concatenate string in Perl: Concatenating a string is very important feature in any computer programming language. Perl provide […]

How to write excel sheet using Perl module

How to write excel sheet using Perl module : Microsoft excel is an application, which we can use to store […]

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *