Updated July 1, 2023
Introduction to Perl JSON
Perl JSON is a feature that enables the conversion of Perl script functions and data structures into JSON UTF-8 encoded binary strings. These values conform to pre-defined formats that are supported across various programming languages using specific modules. The data is automatically converted into JSON strings, and the parsing of these strings returns reference results through the TO_JSON() method. Object classes are converted to JSON using the JSON: parse method.
Syntax:
The Perl scripts have default keywords, variables, and methods for creating applications more sophisticated.
#!/usr/bin/perl
package package name;
use JSON;
$var=("");
$var1=$JSON->default methods(encode,decode,to_json,from_json)($var1);
—some Perl script logic based on the requirement—
The above codes are the basic syntax for utilizing the JSON in scripts.
How JSON work in Perl?
In script, we can convert the data to the JSON string by using some predefined modules it can be achieved with the script. The JSON::PP, JSON::XS, JSON, JSON::Meth, JSON: Syck, JSON, Cpanel::JSON::XS, Mojo::JSON, and JSON::MaybeXS are the modules that have to be implemented with JSON in Perl. So these encoded techniques will be more useful for encrypting the data, which is the secured purpose for web-based applications. Likewise, the decode_json is used for decoding the encrypted values that are decoding the JSON strings. When we use to_json() method, it converts the data structures to the JSON strings, so the from_json() method expects the same JSON string values and tries to parse them, and it returns the reference results. The convert_blessed method is like the other JSON function, which has set the true values, and the scripts can be used for calling the to_json. It converts the object classes to JSON.
Examples
Let us discuss examples of Perl JSON.
Example #1
Code:
#!/usr/bin/perl
package first;
sub new {
my $vars = shift;
my $vars1 = {
examp=> shift,
exampl1 => shift,
examp2 => shift,
examp22 => shift,
examp23 => shift,
examp24 => shift,
examp25 => shift,
};
bless $vars1, $vars;
return $vars1;
}
sub TO_JSON { return { %{ shift() } }; }
package second;
use JSON;
my $JSON = JSON->new->utf8;
$JSON->convert_blessed(4);
$vars2 = new first( "siva", "IT", "TUP", "MAS", "jhv", "ajdgshj", "sbjs", "sdjb", "jhdj","gsdjv", "mshdj", "jkskhb");
$vars3 = $JSON->encode($vars2);
print "Welcome to My Domain your details \n$vars3\n";
Output:
Example #2
Code:
#!/usr/bin/perl
use JSON;
use Data::Dumper;
$vars = '{"ndkj":79841,"bdhkjf":2784,"cdfb":3556,"dkerjhfjk":4556,"ejkhe5krj":874,"jsdhkb":84767,"sdhfjb":467,"sdj":6487}';
$vars1 = decode_json($vars);
print Dumper($vars1);
$vars = '{"ndsfdkj":7945841,"basdasddhkjf":273484,"casdfgdfb":3552346,"dkerjhAS fjk":45556,"ejkhe5krj":8744,"jsdhkb":84767,"sdSDhfjb":467,"sdDj":6487}';
$vars1 = decode_json($vars);
print Dumper($vars1);
$vars = '{"6347j":7789841,"basdhkjf":2784,"cdfb":3556,"dkerjhfjk":4556,"ejkhe5krj":874,"jsdhkb":84767,"sdhfjb":467,"sdj":6487}';
$vars1 = decode_json($vars);
print Dumper($vars1);
$vars = '{"n7367t6kj":79788841,"basdhkjf":276784,"cdfb":3556,"dkerjhfjk":4556,"ejkhe5krj":874,"jsdhkb":84767,"sdhfjb":467,"sdj":6487}';
$vars1 = decode_json($vars);
print Dumper($vars1);
$vars = '{"ndk23787rj":79898741,"bdashkjf":273484,"cdfb":3556,"dkerjhfjk":4556,"ejkhe5krj":874,"jsdhkb":84767,"sdhfjb":467,"sdj":6487}';
$vars1 = decode_json($vars);
print Dumper($vars1);
$vars = '{"nd324fdkj":7980987841,"bdhaskjf":278456,"cdfb":3556,"dkerjhfjk":4556,"ejkhe5krj":874,"jsdhkb":84767,"sdhfjb":467,"sdj":6487}';
$vars1 = decode_json($vars);
print Dumper($vars1);
Output:
In the Second example, we used the decode_json method for decoding the strings after entering the data in the encode_json. We use decoding methods to parse and convert UTF-8 binary format strings into UTF-8 encoded JSON text format. Additionally, we employ a default function called Dumper to print Perl variable values in JSON format.
Example #3
Code:
#!/usr/bin/perl
use JSON;
use utf8;
use strict;
use warnings;
my $vars = '{
"first": {
"strngs1": "duifgjdfj",
"strngs2": "dsvjfh",
"strngs3": "ashfk",
"strngs4": 4354
},
"second": {
"strngs1": "hsdkjb",
"strngs2": "hsdjbv",
"strngs3": "hskbjd",
"strngs4": 4357
},
"third": {
"strngs1": "ashkdfj",
"strngs2": "jasgdjb",
"strngs3": "kshdfb",
"strngs4": 4358
}
}';
my $vars1 = decode_json $vars;
foreach my $vars2 (keys %$vars1) {
print "$vars2\n";
}
print "Welcome To My Domain $vars1->{first}->{strngs2}\n";
Output:
In the third example, we used the variables’ basic JSON output values. By using a forEach loop, we have iterated the keys and values and printed the same in the output console. Here we used keys to iterate the values in the forEach loop.
Conclusion
In conclusion, JSON is the format web users receive after sending the request to the server; the server response is the XML or JSON format. For that, the script matches and uses the JSON format strings in the applications in various ways.
Recommended Articles
This is a guide to Perl JSON. Here we discuss the introduction, syntax, How JSON works in Perl, and examples with code implementation. You may also have a look at the following articles to learn more –