Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

mojo
json
communitycreator

What is the from_json method in Mojo::JSON?

Gutha Vamsi Krishna

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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);
The from_json method example

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.

RELATED TAGS

mojo
json
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring