YUI Library Home

YUI Library Examples: JSON Utility: Adding new object members during parsing

JSON Utility: Adding new object members during parsing

This example shows how to use the reviver parameter in JSON.parse to add new object members and format existing members during the parsing phase.

Choose a currency, then get the data

SKU Item Price (USD) Price (USD)
Click Get Data

The data

The data returned from the server will be a JSON string containing this object structure:

Create a reviver function

We'll contain all the moving parts in the YAHOO.demo.JSONReviver namespace. In it, we'll include the currency exchange rates and a function to reference the rates to add a new member to the JSON response as it is being parsed.

Sending the request and parsing the JSON response

When the Get Data button is clicked, we send an asyncRequest for the JSON data from YUI's Connection Manager. In our success handler, we pass our conversion function to JSON.parse with the response text. The resulting inventory records will have an additional member, convertedPrice. This data is then passed to a UI method to update the inventory table.

Example markup

Full Code Listing

Below is the full source for the example, including the functions responsible for formatting the price and updating the UI.

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

More JSON Utility Resources:

Copyright © 2011 Yahoo! Inc. All rights reserved.

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