Developer Network Home - Help

Yahoo! UI Library: JSON utility

JSON Utility

The JSON Utility is a YUI implementation of Douglas Crockford's work on JSON (JavaScript Object Notation). JSON is a safe, efficient, and reliable data interchange format. The JSON Utility provides methods for working with JSON in JavaScript, providing JSON-to-string and string-to-JSON conversion and validation.

Getting Started

To use the JSON Utility, include the following source files in your web page with the script tag:

YUI dependency configurator.

YUI Dependency Configurator:

Instead of copying and pasting the filepaths above, try letting the YUI dependency Configurator determine the optimal file list for your desired components; the Configurator uses YUI Loader write out the full HTML for including the precise files you need for your implementation.

Note: If you wish to include this component via the YUI Loader, its module name is json. (Click here for the full list of module names for YUI Loader.)

Where these files come from: The files included using the text above will be served from Yahoo! servers; see "Serving YUI Files from Yahoo!" for important information about this service. JavaScript files are minified, meaning that comments and white space have been removed to make them more efficient to download. To use the full, commented versions or the -debug versions of YUI JavaScript files, please download the library distribution and host the files on your own server.

Order matters: As is the case generally with JavaScript and CSS, order matters; these files should be included in the order specified above. If you include files in the wrong order, errors may result.

YAHOO.lang.JSON is a static class that does not require instantiation. Simply access the methods you need from YAHOO.lang.JSON.

Using YAHOO.lang.JSON

The JSON Utility extends YAHOO.lang, providing two static methods used for transforming data to and from JSON string format. These methods are described in the following sections.

Parsing JSON string data into JavaScript data

Pass a JSON string to YAHOO.lang.JSON.parse to convert the JSON string into live data. Optionally provide a second argument to filter or format the parsed object data en route.

You can filter out data or format data during parse by passing a function as the second argument to parse. Values returned from the function will take the place of the original value. Return undefined to omit a key:value pair from the returned object.

A word of caution against using eval

JSON data format is a subset of JavaScript notation, meaning that it is possible to use JavaScript eval to transform JSON data to live data. However, it is unsafe practice to assume that data reaching your code is safe. eval is capable of executing JavaScript's full notation, including calling functions and accessing cookies with all the privileges of a <script>. To ensure that the data is safe, YAHOO.lang.JSON.parse inspects the incoming string (using methods derived from Douglas Crockford's json2.js) and verifies it as valid JSON before giving it the green light to parse.

Stringifying JavaScript data into a JSON string

To convert a JavaScript object (or any permissable value) to a JSON string, pass that object to YAHOO.lang.JSON.stringify and capture the returned string. Note that cyclical references will be converted to null.

Additionally, stringify supports optional whitelist and depth-limit parameters.

About the JSON format

JSON is a lightweight data format based on the object notation of the JavaScript language. It does not require JavaScript to read or write; it is easy to parse by any language and libraries and tools exist in many languages to handle JSON. You'll find several examples of support for JSON data in the YUI Library.

There is a wealth of good information about JSON and its distinction from XML as a data interchange format at Douglas Crockford's site, JSON.org.

JSON data is characterized as a collection of objects, arrays, booleans, strings, numbers, and null. The notation follows these guidelines:

  • Objects begin and end with curly braces ({}).
  • Object members consist of a string key and an associated value separated by a colon ( "key" : VALUE ).
  • Objects may contain any number of members, separated by commas ({ "key1" : VALUE1, "key2" : VALUE2 }).
  • Arrays begin and end with square braces and contain any number of values, separated by commas ([ VALUE1, VALUE2 ]).
  • Values can be a string, a number, an object, an array, or the literals true, false, and null.
  • Strings are surrounded by double quotes and can contain Unicode characters and common backslash escapes ("new\nline").

JSON.org has helpful format diagrams and specific information on allowable string characters.

Here is a simple example of a valid JSON string:

This structure should look familiar to JavaScript programmers (for good reason). Note, however the solidus (forward slash) characters in the "Url" value are escaped.

YUI on Mobile: Using JSON Utility with "A-Grade" Mobile Browsers

About this Section: YUI generally works well with mobile browsers that are based on A-Grade browser foundations. For example, Nokia's N-series phones, including the N95, use a browser based on Webkit — the same foundation shared by Apple's Safari browser, which is found on the iPhone. The fundamental challenges in developing for this emerging class of full, A-Grade-derived browsers on handheld devices are:

  • Screen size: You have a much smaller canvas;
  • Input devices: Mobile devices generally do not have mouse input, and therefore are missing some or all mouse events (like mouseover);
  • Processor power: Mobile devices have slower processors that can more easily be saturated by JavaScript and DOM interactions — and processor usage affects things like battery life in ways that don't have analogues in desktop browsers;
  • Latency: Most mobile devices have a much higher latency on the network than do terrestrially networked PCs; this can make pages with many script, css or other types of external files load much more slowly.

There are other considerations, many of them device/browser specific (for example, current versions of the iPhone's Safari browser do not support Flash). The goal of these sections on YUI User's Guides is to provide you some preliminary insights about how specific components perform on this emerging class of mobile devices. Although we have not done exhaustive testing, and although these browsers are revving quickly and present a moving target, our goal is to provide some early, provisional advice to help you get started as you contemplate how your YUI-based application will render in the mobile world.

More Information:

There are no known concerns with the JSON Utility on mobile browsers at this time.

Support & Community

The YUI Library and related topics are discussed on the on the ydn-javascript mailing list.

In addition, please visit the YUIBlog for updates and articles about the YUI Library written by the library's developers.

Filing Bugs & Feature Requests

The YUI Library's public bug tracking and feature request repositories are located on the YUI SourceForge project site. Before filing new feature requests or bug reports, please review our reporting guidelines.

JSON Utility Cheat Sheet:

Cheat Sheet for the JSON Utility.

Download full set of cheat sheets.

JSON Utility Examples:

YUI JSON Utility on del.icio.us:

Copyright © 2008 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings