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
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
Arrow right icon
€18.99 per month
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
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Ray Rischpater
Arrow right icon
€18.99 per month
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
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781785286902
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

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
€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

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 included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.