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 packages
use 5.010;
use Data::Dumper;
use Mojo::JSON qw(from_json);
# given json text
my $json = '{ "comment" : "I ♥ Educative" }';
# decode the json text
my $hash = from_json $json;
# Print the returned perl value
print Dumper($hash);

Explanation

  • Line 7: We declare and initialize the JSON string, json.
  • Line 10: We decode json, which is not UTF-8 encoded, using the method from_json. We store it in the hash variable.
  • Line 13: We print the contents of the hash using the Dumper function provided by the Data::Dumper module.

Free Resources