Work with JSON format in PERL language

    Work with JSON format in PERL language.

    JSON format

    JSON (JavaScript Object Notation) is a text data format. It is an alternative to the XML format. For example, consider the differences between the JSON and XML formats. Suppose a developer needs to store information about students in the Journal students application. The listing below shows the implementation of data storage using the XML format.

    A similar data structure presented in JSON format will look like this:
            "name": "Петр",
            "surname": "Петров",
            "faculty": "Машиностроительный",
            "group": "М-72",
            "adress": {
                "city": "Москва",
                "street": "Речная",
                "house": "12",
                "apartment": "24"
            "name": "Алексей",
            "surname": "Алексеев",
            "faculty": "Экономический",
            "group": "Э-51",
            "adress": {
                "city": "Москва",
                "street": "Береговая",
                "house": "2",
                "apartment": "14"

    Formulation of the problem

    It is necessary to write a script in Perl language intended for parsing a JSON format data structure. This is necessary for operations performed on data from a JSON format structure.
    JSON and Perl

    To work with the JSON format, the JSON-2.53 library is used:
    use JSON;

    The decodeJSON subroutine, presented below, is designed to convert a JSON format data structure into a Perl language data structure (composed of arrays and hashes of varying degrees of nesting).
    sub decodeJSON {
    	my ($JSONText) = @_;
        my $hashRef = decode_json($JSONText);
        return @$hashRef;

    The encodeJSON routine is designed to convert a Perl data structure into a JSON format data structure.
    sub encodeJSON{
    	my($arrayRef) = @_;
    	$JSONText= JSON->new->utf8->encode($perl_scalar);
    	return $JSONText;


    The result is a Perl data structure, for which the following functionality will be written in the future:
    • Adding elements;
    • Removing items;
    • Editing element data;
    • Search for the required item;

    Also popular now: