What is the from_json method in Mojo::JSON?
Overview
The from_json method decodes the JSON text that is not UTF-8 encoded to Perl value. This method is provided by the Mojo::JSON module, which provides methods to manipulate JSON.
Syntax
from_json $json
Here, $json is the JSON string text that is not UTF-8 encoded.
Returns
The from_json method returns a Perl hash.
Let's take a look at an example.
Example
In the following example, we try to decode the JSON text that is not UTF-8 encoded and print the returned Perl hash.
Code
# import required packagesuse 5.010;use Data::Dumper;use Mojo::JSON qw(from_json);# given json textmy $json = '{ "comment" : "I ♥ Educative" }';# decode the json textmy $hash = from_json $json;# Print the returned perl valueprint Dumper($hash);
Explanation
- Line 7: We declare and initialize the JSON string,
json. - Line 10: We decode
json, which is notUTF-8encoded, using the methodfrom_json. We store it in thehashvariable. - Line 13: We print the contents of the
hashusing theDumperfunction provided by theData::Dumpermodule.