JSON - Analyzing JSON data
You can use the JSON module to parse JSON data. JSON is a language for describing data and can represent data structures such as arrays and hashes. JSON is just a string, so it's portable. It is often used when exchanging data with other programming languages.
# Loading modules and importing functions use JSON qw/encode_json decode_json/;
Use the decode_json function to convert JSON to a Perl data structure.
# Convert JSON to Perl data structures
my $json_in = '[{"name": "Ken", "age": 19}, {"name": "Ken", "age": 25}]';
my $data = decode_json ($json_in);
This translates into a Perl data structure that looks like this:
# JSON
'[{" name ":" Ken ", " age ": 19}, {" name ":" Ken ", " age ": 25}]'
▼
# Perl
[
{
'name' =>'Ken',
'age' => 19
},
{
'name' =>'Ken',
'age' => 25
}
];;
Conversely, use the encode_json function to convert Perl data structures to JSON.
# Convert Perl data structures to JSON my $json_out = encode_json ($data);
Handling of Japanese in JSON module
You need to be a little careful when dealing with Japanese in the JSON module. You need to remember the following:
- decode_json converts from "UTF-8 byte string-JSON" to "decoded string-Perl data"
- encode_json converts from "decoded string-Perl data" to "UTF-8 byte string-JSON"
This relationship is illustrated below.
UTF-8 byte string-JSON UTF-8 byte string-Perl data \\ encode_json is\Conversion to top left\\decode_json is the conversion to the lower right \\ Internal string JSON Internal string-Perl data
The conversion from top left to bottom right is done by decode_json, and the conversion from bottom right to top left is done by encode_json. There is no method to convert up/down or left/right.
For example, it is easy to make a mistake, but if you write the following, it will not work. (Since Japanese is handled in the script, the utf8 pragma is enabled and the script is saved in UTF-8.)
# Common mistakes when dealing with Japanese. decode_json needs to receive UTF-8 byte string
use utf8;
use JSON 'decode_json';
my $json = '{"name": "Masuda"}';
my $data = decode_json ($json);
The reason this doesn't work is that $json has already been converted to an decoded string because the utf8 pragma is enabled. This doesn't work because decode_json takes a byte string as an argument. You need to convert it to a UTF-8 byte string with the encode_utf8 function of the Encode module.
# Correct example when dealing with Japanese
use utf8;
use JSON 'decode_json';
use Encode 'encode_utf8';
my $json = '{"name": "Masuda"}';
# Convert to UTF-8 byte string
$json = encode_utf8 ($json);
my $data = decode_json ($json);
Read the JSON written in the file
Since the JSON module does not have a function to read a file, I will describe how to read from a file.
# Example to read JSON from file
use JSON 'decode_json';
my $file = shift;
my $data = decode_json (get_content ($file));
# Function to get the contents of a file
sub get_content {
my $file = shift;
open my $fh, '<', $file
or die "Can't open file \" $file\":$!";
my $content = do {local $/; <$fh>};
close $fh;
return $content;
}
Perl ABC