The to_json
method is used to encode the Perl value to JSON without UTF-8 encoding it. This method is provided by the module Mojo::JSON
, which provides methods to manipulate JSON.
to_json $hash
Where the hash
is the Perl value.
The to_json
method returns a JSON text.
Let's take a look at an example.
In the following example, we'll try to encode a Perl value without UTF-8 encoding it, and print the returned JSON text.
# import required packagesuse 5.010;use Data::Dumper;use Mojo::JSON qw(to_json);# given Perl valuemy $hash = {comment => 'I ♥ Educative'};# encode the given Perl valuemy $json = to_json $hash;# Print the returned JSONsay $json
In the code snippet above:
hash
which we want to convert to JSON.hash
into JSON without UTF-8 encoding it using the method to_json
.json
.