Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
The JavaScript JSON Cookbook

You're reading from   The JavaScript JSON Cookbook Over 80 recipes to make the most of JSON in your desktop, server, web, and mobile applications

Arrow left icon
Product type Paperback
Published in Jun 2015
Publisher
ISBN-13 9781785286902
Length 192 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (12) Chapters Close

Preface 1. Reading and Writing JSON on the Client FREE CHAPTER 2. Reading and Writing JSON on the Server 3. Using JSON in Simple AJAX Applications 4. Using JSON in AJAX Applications with jQuery and AngularJS 5. Using JSON with MongoDB 6. Using JSON with CouchDB 7. Using JSON in a Type-safe Manner 8. Using JSON for Binary Data Transfer 9. Querying JSON with JSONPath and LINQ 10. JSON on Mobile Platforms Index

Reading and writing JSON in Perl

Perl predates JSON, although there's a good implementation of JSON conversion available from CPAN, the Comprehensive Perl Archive Network.

How to do it...

To begin with, download the JSON module from CPAN and install it. Typically, you'll download the file, unpack it, and then run the following code on a system that already has Perl and make configured:

perl Makefile.PL 
make 
make install

Here's a simple example:

use JSON;
use Data::Dumper;
my $json = '{ "call":"KF6GPE","type":"l","time":"1399371514",
"lasttime":"1418597513","lat": 37.17667,"lng": -122.14650,
"result" : "ok" }';
my %result = decode_json($json);
print Dumper(result);
print encode_json(%result);

Let's look at the interface the JSON module provides.

How it works...

The CPAN module defines the decode_json and encode_json methods to decode and encode JSON respectively. These methods interconvert between Perl objects, such as literal values and associative arrays, and strings containing JSON respectively.

The code begins by importing the JSON and Data::Dumper modules. Next, it defines a single string, $json, which contains the JSON we want to parse.

With the JSON in $json, we define %result to be the associative array containing the objects defined in the JSON, and dump the values in the hash on the next line.

Finally, we re-encode the hash as JSON and print the results to the terminal.

See also

For more information and to download the JSON CPAN module, visit https://metacpan.org/pod/JSON.

You have been reading a chapter from
The JavaScript JSON Cookbook
Published in: Jun 2015
Publisher:
ISBN-13: 9781785286902
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at R$50/month. Cancel anytime