Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
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 Ray Rischpater Profile Icon Brian Ritchie
Arrow right icon
€8.99 €29.99
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1 (2 Ratings)
eBook Jun 2015 192 pages 1st Edition
eBook
€8.99 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Ray Rischpater Profile Icon Brian Ritchie
Arrow right icon
€8.99 €29.99
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1 (2 Ratings)
eBook Jun 2015 192 pages 1st Edition
eBook
€8.99 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€8.99 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
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

Billing Address

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

The JavaScript JSON Cookbook

Chapter 2. Reading and Writing JSON on the Server

In the previous chapter, we looked at JSON handling in some of the most common client-side environments. In this chapter, we will turn our attention to server-side JSON encoding and decoding. We'll look at recipes on how to do this in the following environments:

  • Reading and writing JSON in Clojure
  • Reading and writing JSON in F#
  • Reading and writing JSON in Node.js
  • Reading and writing JSON in PHP
  • Reading and writing JSON in Ruby

Some languages, such as C++ and Java, are used on both client-side and server-side; for these, refer to Chapter 1, Reading and Writing JSON on the Client (one exception is the discussion of JSON in Node.js because Node.js plays a big role in subsequent chapters of this book).

Reading and writing JSON in Clojure

Clojure is a modern Lisp variant running on top of the Java and Microsoft Common Language Runtime (CLR) platforms. As such, you can use the facilities we discussed in Chapter 1, Reading and Writing JSON on the Client, to convert between JSON and objects in the native runtime, but there's a better way, and that is the Clojure's data.json module, available at https://github.com/clojure/data.json.

Getting ready

To begin, you need to specify your dependency in the data.json module. You can do this with the following dependency in your Leiningen file:

[org.clojure/data.json "0.2.5"]

If you're using Maven, you'll want this:

<dependency>
<groupId>org.clojure</groupId>
<artifactId>data.json</artifactId>
<version>0.2.5</version>
</dependency>

Tip

Of course, the version of data.json may change between the time I write this and the time you include it in your project as a dependency. Check...

Reading and writing JSON in F#

F# is a language running on the CLR and .NET that excels in functional and object-oriented programming tasks. Because it's on top of .NET, you can use third-party libraries such as Json.NET (mentioned in Chapter 1, Reading and Writing JSON on the Client) to convert between JSON and CLR objects. However, there's a better way: the open source library F# Data, which creates native data type providers to process data in a number of different structured formats, including JSON.

Getting ready

Begin by getting a copy of the library, available at https://github.com/fsharp/FSharp.Data. Once you download it, you'll need to build it; you can do this by running the build.cmd build batch file that comes with the distribution (for details, see the F# Data website). Alternatively, you can find the same package on NuGet, by choosing Manage NuGet Packages from the Projects menu and searching for F# Data. Once you find it, click on Install. I prefer using NuGet...

Reading and writing JSON with Node.js

Node.js is a JavaScript environment for server-side programming based on the same high-performance JavaScript runtime Google built for Chrome, backed by Joyent. Its high performing and asynchronous programming model makes it an excellent environment for custom web servers and it's used by major companies, including Walmart, in production settings.

Getting ready

Because we'll use Node.js in the next two chapters as well, it's worth pointing out to you how to download and install it, even if your daily server environment is something more like Apache or Microsoft IIS. You'll need to go to http://www.nodejs.org/ and download the installer from the front page. This will install all you need to run Node.js and npm, the package manager used by Node.js.

Tip

After installing on Windows, I had to reboot to get the Windows shell to correctly find the node and npm commands that the Node.js installer installed.

Once you get Node.js installed, we...

Reading and writing JSON in PHP

PHP is a popular server-side scripting environment easily integrated with the Apache and Microsoft IIS web servers. It has native support for simple JSON encoding and decoding.

How to do it...

PHP provides two functions, json_encode and json_decode, to encode and decode JSON respectively.

You can pass primitive types or user-defined classes to json_encode and it returns a string containing the JSON representing the object. For example:

$result = array(
"call" =>"KF6GPE",
"type" =>"l",
"time" =>"1399371514",
"lasttime" =>"1418597513",
"lat" =>37.17667,
"lng" =>-122.14650,
"result" =>"ok");
$json = json_encode($result);

This creates a string $json containing the JSON representation of our associative array.

The json_encode function takes an optional second argument, which lets you specify arguments to the encoder. The arguments...

Reading and writing JSON in Ruby

Ruby provides the json gem for JSON handling. In earlier versions of Ruby, you have to install this gem yourself; it's part of the base installation from Ruby 1.9.2 and onwards.

Getting ready

If you're running an earlier version of Ruby than Ruby 1.9.2, first install the gem with the following command:

gem install json

Note that Ruby's implementation is in C, so installing the gem may require a C compiler. If you don't have one installed on your system, you can install the pure Ruby implementation of the gem using the following command:

gem install json_pure

Regardless of whether you need to install the gem or not, you'll need to include it in your code. To do this, include both rubygems and json or json/pure, depending on which gem you installed; do this using require, like this:

require 'rubygems'
require 'json'

The preceding code handles the former case, while the following code handles the latter:

require 'rubygems...
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

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 : 9781785284359
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
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

Billing Address

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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
The JavaScript JSON Cookbook
€36.99
JavaScript and JSON Essentials
€24.99
JavaScript and JSON Essentials
€24.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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.