You'll need to install the JSON module from CPAN before you can start encoding and decoding JSON with Perl. Follow the procedures below after you've downloaded JSON-2.53.tar.gz or any other recent version.
Function | Libraries |
---|---|
encode_json | Converts a Perl data structure to a binary text encoded in UTF-8. |
decode_json | This function decodes a JSON string. |
to_json | The specified Perl data structure is converted to a json string. |
from_json | Expects a json string and attempts to parse it, returning a reference as a result. |
convert_blessed | Utilize the true value of this function to allow Perl to use the TO JSON method on the object's class to convert an object to JSON. |
The encode json() function in Perl turns a Perl data structure into a binary text encoded in UTF-8.
The following example uses Perl to display arrays under JSON.
This will provide the following result when executed:
The following example demonstrates how to convert Perl objects to JSON.
On executing, it will produce the following result −
The decode json() function in Perl is used to decode JSON. This function converts the value decoded from json into a Perl type.
The following example demonstrates how to decode JSON objects using Perl. If you don't already have it on your PC, you'll need to install the Data::Dumper module.
When you run it, you'll get the following result:
|