Book Image

JavaScript JSON Cookbook

By : Ray Rischpater, Brian Ritchie, Ray Rischpater
Book Image

JavaScript JSON Cookbook

By: Ray Rischpater, Brian Ritchie, Ray Rischpater

Overview of this book

Table of Contents (17 chapters)
JavaScript JSON Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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.