### JSON Data Set

#### Description

The Spry.Data.JSONDataSet class dervies from the HTTPSourceDataSet class so that it can load and process JSON data. The JSONDataSet follows a fixed set of rules for flattening JSON data. If the data to be flattened is not at the top-level of the JSON data structure, a path can be used to identify the data within the JSON structure that should be flattened. You can see how the JSONDataSet flattens various JSON data structures here.

#### File

SpryJSONDataSet.js

#### Requires

SpryData.js

#### Inheritance

Notifier -->DataSet --> HTTPSourceDataSet --> JSONDataSet

### JSONDataSet constructor

#### Description

Spry.Data.JSONDataSet is the constructor function for the JSON Data Set.

#### Format

Spry.Data.JSONDataSet(url, options)

#### Arguments

- url - The URL to the data to be loaded. This URL can be absolute, or relative to the HTML document that created the data set.
- options - An optional argument which if specified, must be an object with the option names as properties with values. Properties that can be specified are:
  - path
    - String.
    - Specifies the path to the data within the JSON structure. The path is expressed as a set of property names on the objects, separated by dots. For example, given the following JSON format:

          {
            "results":
            {
              "count": 3,
              "data":
              [
                { "color": "orange", "category": "sedan" },
                { "color": "yellow", "category": "sedan" },
                { "color": "red",    "category": "coupe" }
              ]
            }
          }

      To flatten the structure in the "data" array, you would specify a path of "results.data". Or if you were only interested in extracting out all of the specified "category" values, you could specify a path of "results.data.category".

  - pathIsObjectOfArrays
    - Boolean.
    - If true, the data that the data set will flatten is an object that has properties that all contain arrays:

          {
            "data":
            {
              "firstname": [ "John",  "Jane", "Ann"  ],
              "lastname":  [ "Smith", "Doe",  "Hunt" ]
              "id":        [ "3001",  "4532", "5462" ]
            }
          }

      If the data set was given a path of "data" to flatten, the data set would treat the "data" structure as if it were expressed in this manner:

          {
            "data":
            [
              { "firstname": "John", "lastname": "Smith", "id": "3001" },
              { "firstname": "Jane", "lastname": "Doe",   "id": "4532" },
              { "firstname": "Ann",  "lastname": "Hunt",  "id": "5462" }
            ]
          }

  - subPaths
    - String or Array of Strings.
    - A path string or array of path strings that are relative to the path being flattened by the data set. This option allows the developer to add nested structures to the list of things to flatten. To see a live usage of this, click here.
  - Any option for the HTTPSourceDataSet or DataSet.

#### Returns

N/A

#### Example

    // An example of creating a JSON data set that flattens the top-level
    // structure of the JSON data.

    var ds = new Spry.Data.JSONDataSet("search.php?id=100");

    // An example of creating a JSON data set that uses some DataSet
    // and HTTPSourceDataSet options.

    var ds = new Spry.Data.JSONDataSet("search.php?id=100", { useCache: false, loadInterval: 1000 });

    // An example of creating a JSON data set that specifies a path
    // within the JSON data structure to the actual data.

    var ds = new Spry.Data.JSONDataSet("search.php?id=100", { path: "results.data" });

    // An example of creating a JSON data set that specifies a subPath to
    // include in the flattening process.

    var ds = new Spry.Data.JSONDataSet("search.php?id=100", { path: "results.employees.employee", subPaths: "reviews.review.date" });

    // An example of creating a JSON data set that specifies multiple
    // subPaths to include in the flattening process.

    var ds = new Spry.Data.JSONDataSet("search.php?id=100", { path: "results.employees.employee", subPaths: [ "reviews.review.date", "reviews.review.increase" ] });

#### Sample

JSONDataSetSample.html

### getDocument

#### Description

This function returns the JSON string that was used by the data set to extract its data.

#### Format

getDocument()

#### Arguments

N/A

#### Returns

String or null. The JSON string or null;

#### Example

    var jsonStr = ds.geDocument();

### getPath

#### Description

This function returns the path to be used for finding the data to flatten within the JSON data structure.

#### Format

getPath()

#### Arguments

N/A

#### Returns

String or null. Returns the path that will be used to extract the data for flattening out of the JSON data. This may also return a null if no path was specified at construction time.

#### Example

    var path = ds.getPath();

### loadDataIntoDataSet

#### Description

This function is not meant to be called directly. The JSONDataSet overrides the HTTPSourceDataSet version of this function to add the ability to parse and flatten the JSON data.

#### Format

loadDataIntoDataSet(jsonString)

#### Arguments

jsonString - The raw JSON string that will be eval'd to create the JSON data structure.

#### Returns

N/A

#### Example

N/A

### setPath

#### Description

This function sets the path to use to extract data out of the JSON data structure. If the JSON data has already been loaded, the JSONDataSet automatically applies the new object path to re-generate the data for the data set.

#### Format

setPath()

#### Arguments

N/A

#### Returns

N/A

#### Example

     ds.setPath("employees.employee");

Copyright © 2007. Adobe Systems Incorporated.
All rights reserved.