Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

mojo
communitycreator
json

What is the decode_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 decode_json method is used to decode JSON to Perl value. This method is provided by the module Mojo::JSON which provides methods to manipulate JSON.

Syntax

decode_json $json;

Where $json is the JSON string.

Return value

The method returns a Perl hash.

Example

In the following example, we will try to decode the JSON string and print the returned Perl hash.

# import required packages
use 5.010;
use Data::Dumper;
use Mojo::JSON qw(decode_json);
# given json string
my $json = '{"name":"John","scores":[40,90]}';
# decode the given json
my $hash = decode_json $json;
# Print the returned hash
print Dumper($hash);
decode_json example

Explanation

In the above code snippet,

  • Line 7: We declare and initialize the JSON string json.
  • Line 10: We decode the JSON string, json, using the decode_json method, and 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
communitycreator
json

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