Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
The JavaScript JSON Cookbook
The JavaScript JSON Cookbook

The JavaScript JSON Cookbook: Over 80 recipes to make the most of JSON in your desktop, server, web, and mobile applications

Arrow left icon
Profile Icon Brian Ritchie Profile Icon Ray Rischpater
Arrow right icon
£36.99
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1 (2 Ratings)
Paperback Jun 2015 192 pages 1st Edition
eBook
£7.99 £29.99
Paperback
£36.99
Subscription
Free Trial
Renews at £16.99p/m
Arrow left icon
Profile Icon Brian Ritchie Profile Icon Ray Rischpater
Arrow right icon
£36.99
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1 (2 Ratings)
Paperback Jun 2015 192 pages 1st Edition
eBook
£7.99 £29.99
Paperback
£36.99
Subscription
Free Trial
Renews at £16.99p/m
eBook
£7.99 £29.99
Paperback
£36.99
Subscription
Free Trial
Renews at £16.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

The JavaScript JSON Cookbook

Chapter 1. Reading and Writing JSON on the Client

In this chapter, we will cover the following recipes:

  • Reading and writing JSON in JavaScript
  • Reading and writing JSON in C++
  • Reading and writing JSON in C#
  • Reading and writing JSON in Java
  • Reading and writing JSON in Perl
  • Reading and writing JSON in Python

In addition to reading and writing JSON in Python, we will begin by showing you a brief review of JSON formatting to help set the stage for what follows in this book.

Introduction

JSON stands for JavaScript Object Notation. It's an open standard to represent data as attributes with values. Originally derived from the JavaScript syntax (hence its name) for use in web applications as an alternative to the more verbose and structured Extensible Markup Language (XML), it is now used for data serialization and transport in many standalone and web applications.

JSON provides an ideal means to encapsulate data between the client and server. In this first chapter, you will learn how to work with JSON in languages specified at the beginning of this chapter.

These languages are often used for client-side development, which is what we will focus on here. We'll look more at server-side languages in Chapter 2, Reading and Writing JSON on the Server.

Let's take a look at some JSON returned by the web API, available at http://www.aprs.fi, and modified a bit by me to make the example clear (later, in Chapter 4, Using JSON in AJAX Applications with jQuery and AngularJS, you'll learn how to fetch this data yourself using a web browser and JavaScript):

{
  "command":"get",
  "result":"ok",
  "what":"loc",
  "found":2,
  "entries":[
    {
      "class":"a",
      "name":"KF6GPE",
      "type":"l",
      "time":"1399371514",
      "lasttime":"1418597513",
      "lat":37.17667,
      "lng":-122.14650,
      "symbol":"\/-",
      "srccall":"KF6GPE",
    },
    {
      "class":"a",
      "name":"KF6GPE-7",
      "type":"l",
      "time":"1418591475",
      "lasttime":"1418591475",
      "lat":37.17633,
      "lng":-122.14583,
      "symbol":"\\K",
      "srccall":"KF6GPE-7",
    }
  ]
}

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

There are a few things to notice about this example:

  • The data is organized into attributes and values, each separated by a colon. (Note that a JSON document can also be a single value, such as a string, float, integer, or Boolean value.)
  • Attributes appear as character strings enclosed by double quotes on the left-hand side of a colon.
  • Values are on the right side of the colon and can be the following:
    • Character strings (enclosed in double quotes) such as KF6GPE
    • Numbers (either integers or floating point) such as 2 or 37.17667
    • Arrays (comma-delimited values contained in square brackets), such as the value for entries
    • Whole objects consisting of more attributes and values, such as the two-array values in the entries value
    • Alternatively (although this example doesn't show it), the Boolean values true and false
  • Note that many other kinds of values, such as date/time pairs or individual characters are not supported by JSON.
  • Although it's not entirely clear from this example, whitespace is insignificant. There's no need to have each pair on its own line, for example, and the indentation is completely arbitrary.

The attribute-name-attribute-value property of JSON, along with the ability to nest values and represent arrays, gives JSON a lot of flexibility. You can represent a lot of common objects using JSON, including most objects that don't have a lot of binary data (For ideas on how to represent binary data using JavaScript and JSON, see Chapter 8, Using JSON for Binary Data Transfer). This includes primitive values (self-documenting because each value is accompanied by an attribute), flat objects with simple values including maps, and arrays of simple or complex objects.

The self-documenting nature of JSON makes it an ideal choice for data transport as you develop new objects, despite its lack of support for comments as you might find in XML. Its plaintext nature makes it amenable to compression over the wire using popular compression schemes such as gzip (available inside most web servers and web clients), and its format is easier for humans to read than the more verbose XML.

Tip

Note that JSON documents are inherently trees, and thus, do not have support for cyclical data structures, such as graphs, where a node points to another node in the same data structure.

If you create such a data structure using the native representation in the programming language you're using and try to convert that to JSON, you'll get an error.

Reading and writing JSON in JavaScript

JSON originated as a means to carry data between web servers and JavaScript, so let's begin with a simple code snippet that reads and writes JSON in JavaScript in a web browser. We'll show the entirety of a web application using AJAX and JSON in Chapter 4, Using JSON in AJAX Applications with jQuery and AngularJS; what follows is how to obtain a JavaScript object from JSON and how to create a JSON string from a JavaScript object.

Getting ready

You'll need a way to edit the JavaScript and run it in your browser. In this example, and nearly all examples in this book, we'll use Google Chrome for this. You can download Google Chrome at https://www.google.com/chrome/browser. Once you install Google Chrome, you'll want to activate the JavaScript console by clicking on the Customize and control Doodle Chrome icon on the right-hand side, which looks like this:

Getting ready

Then, you'll want to go to More Tools | JavaScript console. You should see a JavaScript console on the side of the web page, like this:

Getting ready

If you prefer key commands, you can also use Ctrl + Shift + J on Windows and Linux, or control + option + J on a Macintosh.

From here, you can enter JavaScript on the lower right-hand corner and press Enter (return on a Mac OS X system) to evaluate the JavaScript.

How to do it...

Modern web browsers, such as Chrome, define a JSON object in the JavaScript runtime that can convert the string data containing JSON to JavaScript objects, and convert a JavaScript object to JSON. Here's a simple example:

>var json = '{"call":"KF6GPE","type":"l","time":
"1399371514","lasttime":"1418597513","lat":37.17667,"lng":
-122.14650,"result" : "ok" }';
<- "{ "call":"KF6GPE","type":"l","time":"1399371514",
"lasttime":"1418597513","lat":37.17667,"lng":-122.14650,
"result" : "ok" }"
>var object = JSON.parse(json);
<- Object {call:"KF6GPE",type:"l",time:"1399371514",
lasttime:"1418597513",lat:37.17667, lng:-122.14650,result: "ok"}
> object.result
<- "ok"
>var newJson = JSON.stringify(object);
<- "{ "call":"KF6GPE","type":"l","time":"1399371514",
"lasttime":"1418597513","lat": 37.17667,"lng": -122.14650,
"result" : "ok" }"

Note

In this and subsequent JavaScript examples, the text you type in the JavaScript console is preceded by a > symbol, while what the JavaScript console prints is anything beginning with <- symbol.

How it works...

Chrome and other modern web browsers define the JSON object, which has methods to convert between strings containing JSON and JavaScript objects.

In the previous example, we begin by setting the value of the json variable to a simple JSON expression consisting of one attribute result with the value ok. The JavaScript interpreter returns the resulting value of the variable json.

The next line uses the JSON method parse to convert the JSON string referenced by json into a JavaScript object:

>var object = JSON.parse(json);
<- Object { call:"KF6GPE", type:"l", time:"1399371514", lasttime:"1418597513", lat:37.17667, lng:-122.14650, result: "ok"}

You can then access any of the values in the object, just as you would any other JavaScript object; it is, after all, just an object:

> object.result;
<- "ok"

Finally, if you need to convert an object to JSON, you can do that with the JSON method stringify:

>var newJson = JSON.stringify(object);
<- "{ "call":"KF6GPE","type":"l","time":"1399371514",
"lasttime":"1418597513","lat": 37.17667,"lng": -122.14650,
"result" : "ok" }"

There's more...

You should know two things about these methods. First of all, parse will throw an exception if the JSON you pass is malformed, or isn't JSON at all:

>JSON.parse('{"result" = "ok" }')
<- VM465:2 Uncaught SyntaxError: Unexpected token =

The errors aren't very helpful but better than nothing if you're debugging JSON sent by a less-than-fully compliant and debugged JSON encoder.

Second, very old web browsers may not have a JSON object with these methods. In that case, you can use the JavaScript function eval after wrapping the JSON in parenthesis, like this:

>eval('('+json+')')
<- Object {result: "ok"}

The eval function evaluates the string you pass as JavaScript, and the JSON notation is really just a subset of JavaScript. However, you should avoid using eval whenever you can for a few reasons. First, it's often slower than the methods provided by the JSON object. Second, it's not safe; your string might contain malicious JavaScript that can crash or otherwise subvert your JavaScript application, which is not a threat you should take lightly. Use the JSON object whenever it's available. Third, you can use the parse and stringify methods to handle simple values, such as Booleans, numbers, and strings; you're not limited to the key-value pairs in the previous example. If all I wanted to do was pass a Boolean (such as "the transaction succeeded!"), I might just write the following:

var jsonSuccess = 'true';
<- "true"
> var flag = JSON.parse(jsonSuccess);

Finally, it's worth pointing out that both the parse and stringify methods to JSON take an optional replacer function, which is invoked on every key and value in the object being serialized or deserialized. You can use this function to perform on-the-fly data conversions as the JSON is being parsed; for example, you can use it to convert between the string representation of a date and the number of seconds since midnight at the start of the epoch, or to correct the capitalization of strings. I could use a replacer function for either side of the transformation, as shown in the following code, to make the call field lowercase:

> var object = JSON.parse(json, function(k, v) {
  if ( k == 'call') return v.toLowerCase();
});
<- Object { call:"kf6gpe", type:"l", time:"1399371514",
lasttime:"1418597513", lat:37.17667, lng:-122.14650, result: "ok"}

You can also return undefined to remove an item from the results; to omit the type field from the JSON I generate, I can execute the following:

> var newJson = JSON.stringify(object, function (k, v) {
  if k == 'type') return undefined;
});
<- "{ "call":"KF6GPE","time":"1399371514","lasttime":
"1418597513","lat": 37.17667,"lng": -122.14650, "result" : "ok" 
}"

Reading and writing JSON in C++

C++ is a language that long-predates JSON, but is still relevant for many projects. There's no native support for JSON in C++ but there are a number of libraries that provide support for working with JSON. Perhaps the most widely used is JsonCpp, available from GitHub at https://github.com/open-source-parsers/jsoncpp. It's licensed under the MIT license or public domain if you so desire, so there are virtually no limitations on its use.

Getting ready

To use JsonCpp, you need to first go to the website and download the zip file with the entire library. Once you do so, you need to integrate it with your application's source code.

How you integrate it with your application's source code differs from platform to platform, but the general process is this:

  1. Create an amalgamated source and header for the library using the instructions on the website. To do this, you'll need to have JsonCpp downloaded and Python 2.6 or later installed. From the top level directory of JsonCpp, run python amalgamate.py.
  2. Include the include file dist/json/json.h in any file where you want to use the JsonCpp library.
  3. Include the source file dist/jsoncpp.cpp in your project's make file or build system.

Once you do this, you should have access to the JsonCpp interface in any file that includes the json/json.h header.

How to do it...

Here's a simple C++ application that uses JsonCpp to convert between std::string containing some simple JSON and a JSON object:

#include <string>
#include <iostream>
#include "json/json.h"

using namespace std;

int main(int argc, _TCHAR* argv[])
{
  Json::Reader reader;
  Json::Value root;

  string json = "{\"call\": \"KF6GPE\",\"type\":\"l\",\"time\":
  \"1399371514\",\"lasttime\":\"1418597513\",\"lat\": 37.17667,
  \"lng\": -122.14650,\"result\":\"ok\"}";

  bool parseSuccess = reader.parse(json, root, false);

  if (parseSuccess)
  {
    const Json::Value resultValue = root["result"];
    cout << "Result is " << resultValue.asString() << "\n";
  }

  Json::StyledWriter styledWriter;
  Json::FastWriter fastWriter;
  Json::Value newValue;
  newValue["result"] = "ok";

  cout << styledWriter.write(newValue) << "\n";
  cout << fastWriter.write(newValue) << "\n";

  return 0;
}

How it works...

This example begins by including the necessary includes, including json/json.h, which defines the interface to JsonCpp. We explicitly reference the std namespace for brevity, although don't do so for the Json namespace, in which JsonCpp defines all of its interfaces.

The JsonCpp implementation defines Json::Reader and Json::Writer, specifying the interfaces to JSON readers and writers, respectively. In practice, the Json::Reader interface is also the implementation of a JSON class that can read JSON, returning its values as Json::Value. The Json::Writer variable just defines an interface; you'll want to use a subclass of it such as Json::FastWriter or Json::StyledWriter to create JSON from Json::Value objects.

The previous listing begins by defining Json::Reader and Json::Value; we'll use the reader to read the JSON we define on the next line and store its value in the Json::Value variable root. (Presumably your C++ application would get its JSON from another source, such as a web service or local file.)

Parsing JSON is as simple as calling the reader's parse function, passing the JSON and Json::Value into which it will write the JSON values. It returns a Boolean, which will be true if the JSON parsing succeeds.

The Json::Value class represents the JSON object as a tree; individual values are referenced by the attribute name in the original JSON, and the values are the values of those keys, accessible through methods such as asString, which returns the value of the object as a native C++ type. These methods of Json::Value includes the following:

  • asString, which returns std::string
  • asInt, which returns Int
  • asUInt, which returns UInt
  • asInt64, which returns Int64
  • asFloat, which returns float
  • asDouble, which returns double
  • asBool, which returns bool

In addition, the class provides operator[], letting you access array elements.

You can also query a Json::Value object to determine its type using one of these methods:

  • isNull, which returns true if the value is null
  • isBool, which returns true if the value is bool
  • isInt, which returns true if the value is Int
  • isUInt, which returns true if the value is UInt
  • isIntegral, which returns true if the value is an integer
  • isDouble, which returns true if the value is double
  • isNumeric, which returns true if the value is numeric
  • isString, which returns true if the value is a string
  • isArray, which returns true if the value is an array
  • isObject, which returns true if the value is another JSON object (which you can decompose using another Json::Value value)

At any rate, our code uses asString to fetch the std::string value encoded as the result attribute, and writes it to the console.

The code then defines Json::StyledWriter and Json::FastWriter to create some pretty-printed JSON and unformatted JSON in strings, as well as a single Json::Value object to contain our new JSON. Assigning content to the JSON value is simple because it overrides the operator[] and operator[]= methods with the appropriate implementations to convert standard C++ types to JSON objects. So, the following line of code creates a single JSON attribute/value pair with the attribute set to result, and the value set to ok (although this code doesn't show it, you can create trees of JSON attribute-value pairs by assigning JSON objects to other JSON objects):

newValue["result"] = "ok";

We first use StyledWriter and then FastWriter to encode the JSON value in newValue, writing each string to the console.

Of course, you can also pass single values to JsonCpp; there's no reason why you can't execute the following code if all you wanted to do was pass a double-precision number:

Json::Reader reader;
Json::Value piValue;

string json = "3.1415";
bool parseSuccess = reader.parse(json, piValue, false);
  double pi = piValue.asDouble();

See also

For the documentation for JsonCpp, you can install doxygen from http://www.stack.nl/~dimitri/doxygen/ and run it over the doc folder of the main JsonCpp distribution.

There are other JSON conversion implementations for C++, too. For a complete list, see the list at http://json.org/.

Reading and writing JSON in C#

C# is a common client-side language for rich applications as well as for writing the client implementation of web services running on ASP.NET. The .NET library includes JSON serialization and deserialization in the System.Web.Extensions assembly.

Getting ready

This example uses the built-in JSON serializer and deserializer in the System.Web.Extensions assembly, one of the many .NET libraries that are available. If you've installed a recent version of Visual Studio (see https://www.visualstudio.com/en-us/downloads/visual-studio-2015-downloads-vs.aspx), it should be available. All you need to do to use this assembly is include it in the assemblies your application references in Visual Studio by right-clicking the References item in your project, choosing Add Reference, and scrolling down to System.Web.Extensions in the Framework Assemblies list.

How to do it...

Here's a simple application that deserializes some JSON, as a dictionary of attribute-object pairs:

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace JSONExample
{
    public class SimpleResult
    {
        public string result;
    }

    class Program
    {
        static void Main(string[] args)
        {
            JavaScriptSerializer serializer = 
            new System.Web.Script.Serialization.
            JavaScriptSerializer();

            string json = @"{ ""call"":""KF6GPE"",""type"":
""l"",""time"":""1399371514"",""lasttime"":""1418597513"",
""lat"": 37.17667,""lng\": -122.14650,""result"": ""ok"" }";

dynamic result = serializer.DeserializeObject(json);
            foreach (KeyValuePair<string, object> entry in result)
            {
                var key = entry.Key;
                var value = entry.Value as string;
Console.WriteLine(String.Format("{0} : {1}", 
key, value));
            }
            Console.WriteLine(serializer.Serialize(result));

            var anotherResult = new SimpleResult { result="ok" };
            Console.WriteLine(serializer.Serialize(
            anotherResult));
        }
    }
}

How it works...

The System.Web.Extensions assembly provides the JavaScriptSerializer class in the System.Web.Script.Serialization namespace. This code begins by defining a simple class, SimpleResult, which we'll encode as JSON in our example.

The Main method first defines a JavaScriptSerializer instance, and then string containing our JSON. Parsing the JSON is as easy as calling the JavaScriptSerializer instance's DeserializeObject method, which returns an object whose type is determined at run-time based on the JSON you pass.

Tip

You can also use DeserializeObject to parse JSON in a type-safe manner, and then the type of the returned object matches the type you pass to the method. I'll show you how to do this in Chapter 7, Using JSON in a Type-safe Manner.

DeserializeObject returns a Dictionary of key-value pairs; the keys are the attributes in the JSON, and the values are objects representing the values of those attributes. In our example, we simply walk the keys and values in the dictionary, printing each. Because we know the type of the value in the JSON, we can simply cast it to the appropriate type (string, in this case) using the C# as keyword; if it wasn't string, we'd receive the value null. You can use as or the type inference of C# to determine the type of unknown objects in your JSON, making it easy to parse JSON for which you lack strict semantics.

The JavaScriptSerializer class also includes a Serialize method; you can either pass it as a dictionary of attribute-value pairs, as we do with our deserialized result, or you can pass it as an instance of a C# class. If you pass it as a class, it'll attempt to serialize the class by introspecting the class fields and values.

There's more...

The JSON implementation that Microsoft provides is adequate for many purposes, but not necessarily the best for your application. Other developers have implemented better ones that typically use the same interface as the Microsoft implementation. One good choice is Newtonsoft's Json.NET, which you can get at http://json.codeplex.com/ or from NuGet in Visual Studio. It supports a wider variety of .NET platforms (including Windows Phone), LINQ queries, XPath-like queries against the JSON, and is faster than the Microsoft implementation. Using it is similar to using the Microsoft implementation: install the package from the Web or NuGet, add a reference of the assembly to your application, and then use the JsonSerializer class in the NewtonSoft.Json namespace. It defines the same SerializeObject and DeserializeObject methods that the Microsoft implementation does, making switching to this library easy. James Newton-King, the author of Json.NET, makes it available under the MIT license.

As with other languages, you can also carry primitive types through the deserialization and serialization process. For example, after evaluating the following code, the resulting dynamic variable piResult will contain a floating-point number, 3.14:

string piJson = "3.14";
dynamic piResult = serializer.DeserializeObject(piJson);

See also

As I previously hinted, you can do this in a type-safe manner; we'll discuss more of this in Chapter 7, Using JSON in a Type-safe Manner. You'll do this using the generic method DeserializeObject<>, passing a type variable of the type you want to deserialize into.

Reading and writing JSON in Java

Java, like C++, predates JSON. Oracle is presently working on adding JSON support to Java, but in the meantime, several implementations providing JSON support are available on the Web. Similar to the C++ implementation you saw previously in this chapter, you can convert between JSON and Java using a third-party library; in this case, packaged as a Java archive (JAR) file, whose implementation typically represents JSON objects as a tree of named objects.

Perhaps the best Java implementation of JSON parsing is Gson, available from Google at http://code.google.com/p/google-gson/ licensed under the Apache License 2.0.

Getting ready

First, you'll need to get Gson; you can do this by doing a read-only checkout of the repository using SVN over HTTP with SVN by using the following command:

svn checkout http://google-gson.googlecode.com/svn/trunk/google-gson-read-only

Of course, this assumes that you have a Java development kit (http://www.oracle.com/technetwork/java/javase/downloads/index.html) and SVN (TortoiseSVN is a good client for Windows available at http://tortoisesvn.net/downloads.html) installed. Many Java IDEs include support for SVN.

Once you check out the code, follow the instructions that come with it to build the Gson JAR file, and add the JAR file to your project.

How to do it...

To begin, you need to create a com.google.gson.Gson object. This class defines the interface you'll use to convert between JSON and Java:

Gson gson = new com.google.gson.Gson(); 
String json = "{\"call\": \"KF6GPE\", \"type\": \"l\", \"time\":
\"1399371514\", \"lasttime\": \"1418597513\", \"lat\": 37.17667,
\"lng\": -122.14650,\"result\":\"ok\"}";
com.google.gson.JsonObject result = gson.fromJson(json, 
JsonElement.class).getAsJsonObject(); 

The JsonObject class defines the top-level object for containing a JSON object; you use its get and add methods to get and set attributes, like this:

JsonElement result = result.get("result").getAsString();

The Gson library uses the JsonElement class to encapsulate a single JSON value; it has the following methods that let you get the value contained in JsonElement as a plain Java type:

  • getAsBoolean, which returns the value as Boolean
  • getAsByte, which returns the value as byte
  • getAsCharacter, which returns the value as char
  • getAsDouble, which returns the value as double
  • getAsFloat, which returns the value as float
  • getAsInt, which returns the value as int
  • getAsJsonArray, which returns the value as JsonArray
  • getAsJsonObject, which returns the value as JsonObject
  • getAsLong, which returns the value as long
  • getAsShort, which returns the value as short
  • getAsString, which returns the value as String

You can also learn about the type in JsonElement using one of the following methods:

  • isJsonArray, which returns true if the element is an array of objects
  • isJsonNull, which returns true if the element is null
  • isJsonObject, which returns true if the element is a composite object (another JSON tree) instead of a single type
  • isJsonPrimitive, which returns true if the element is a primitive type, such as a number or string

There's more…

You can also convert instances of your classes directly to JSON, writing something like this:

public class SimpleResult {
    public String result;
}

// Elsewhere in your code…
Gson gson = new com.google.gson.Gson(); 
SimpleResult result = new SimpleResult;
result.result = "ok";
String json = gson.toJson(result);	

This defines a class SimpleResult, which we use to create a single instance, and then use the Gson object instance to convert to a string containing the JSON using the Gson method toJson.

Finally, because JsonElement encapsulates a single value, you can also handle simple values expressed in JSON, like this:

Gson gson = new com.google.gson.Gson(); 
String piJson = "3.14";
double result = gson.fromJson(piJson, 
JsonElement.class).getAsDouble(); 

This converts the primitive value 3.14 in JSON to a Java double.

See also

Like the C# example, you can convert directly from JSON to a plain old Java object (POJO) in a type-safe manner. You'll see how to do this in Chapter 7, Using JSON in a Type-safe Manner.

There are other JSON conversion implementations for Java, too. For a complete list, see the list at http://json.org/.

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.

Reading and writing JSON in Python

Python has had native support for JSON since Python 2.6 through the json module. Using the module is as simple as using the import statement to import the module and then accessing the encoder and decoder through the json object that it defines.

Getting ready

Simply enter the following in your source code to be able to reference the JSON facility:

import json

How to do it...

Here's a simple example from the Python interpreter:

>>> import json
>>>json = '{ "call":"KF6GPE","type":"l","time":"1399371514",
"lasttime":"1418597513","lat": 37.17667,"lng": -122.14650,
"result" : "ok" }'
u'{"call":"KF6GPE","type":"l","time":"1399371514",
"lasttime":"1418597513","lat": 37.17667,"lng": -122.14650,
"result": "ok" }'
>>>result = json.loads(json)
{u'call':u'KF6GPE',u'type':u'l',u'time':u'1399371514',
u'lasttime':u'1418597513',u'lat': 37.17667,u'lng': -122.14650,u'result': u'ok'}
>>> result['result']
u'ok'
>>> print json.dumps(result)
{"call":"KF6GPE","type":"l","time":"1399371514",
"lasttime":"1418597513","lat": 37.17667,"lng": -122.14650,
"result":"ok"}
>>> print json.dumps(result, 
...                  indent=4)
{
"call":"KF6GPE",
"type":"l",
"time":"1399371514",
"lasttime":"1418597513",
"lat": 37.17667,
"lng": -122.14650,
    "result": "ok"
}

Let's look at loads and dumps further.

How it works...

Python has great support for associative arrays through its object hierarchy. The json module offers a json object with loads and dumps method that convert from JSON in text strings to associative arrays, and from associative arrays to JSON in text strings. If you're familiar with the Python marshal and pickle modules, the interface is similar; you use the loads method to obtain a Python object from its JSON representation and the dumps method to convert an object into its JSON equivalent.

The previous listing does just this. It defines a variable j that contains our JSON, and then obtains a Python object result using json.loads. Fields in the JSON are accessible as named objects in the resulting Python object. (Note that we can't call our JSON string json because it would shadow the definition of the interface to the module.)

To convert to JSON, we use the json.dumps method. By default, dumps creates a compact machine-readable version of JSON with minimum whitespace; this is best used for over-the-wire transmissions or for storage in a file. When you're debugging your JSON, it helps to pretty-print it with indentation and some whitespace around separators; you can do this using the optional indent and separators arguments. The indent argument indicates the number of spaces that each successive nested object should be indented in the string, and separators indicates the separators between each object and between each attribute and value.

See also

For more documentation on the json module, see the Python documentation at https://docs.python.org/2/library/json.html.

Left arrow icon Right arrow icon

Description

If you're writing applications that move structured data from one place to another, this book is for you. This is especially true if you've been using XML to do the job because it's entirely possible that you could do much of the same work with less code and less data overhead in JSON. While the book's chapters make some distinction between the client and server sides of an application, it doesn't matter if you're a frontend, backend, or full-stack developer. The principles behind using JSON apply to both the client and the server, and in fact, developers who understand both sides of the equation generally craft the best applications.

What you will learn

  • Learn to use JSON in a type-safe way to avoid common data typing errors Exchange data with NoSQL databases such as MongoDB and CouchDB with JSON Make the most of JSON in AJAX web applications, with or without a framework Exchange data between servers and mobile applications running on Android and iOS with JSON Practical examples to show you how to leverage JSON from a variety of application domains
Estimated delivery fee Deliver to Great Britain

Standard delivery 1 - 4 business days

£4.95

Premium delivery 1 - 4 business days

£7.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 25, 2015
Length: 192 pages
Edition : 1st
Language : English
ISBN-13 : 9781785286902
Languages :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Great Britain

Standard delivery 1 - 4 business days

£4.95

Premium delivery 1 - 4 business days

£7.95
(Includes tracking information)

Product Details

Publication date : Jun 25, 2015
Length: 192 pages
Edition : 1st
Language : English
ISBN-13 : 9781785286902
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
£16.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
£169.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just £5 each
Feature tick icon Exclusive print discounts
£234.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just £5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total £ 86.97
JavaScript and JSON Essentials
£24.99
JavaScript and JSON Essentials
£24.99
The JavaScript JSON Cookbook
£36.99
Total £ 86.97 Stars icon
Banner background image

Table of Contents

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

Customer reviews

Rating distribution
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
(2 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 100%
Anthony Davis Aug 13, 2016
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
This book was absolutely terrible. I've never purchased a technical book that was so useless.
Amazon Verified review Amazon
oldcomputer Jan 20, 2017
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I don't understand how this is a book.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela