YUI 3.x Home -

YUI Library Examples: DataSchema Utility: DataSchema.JSON

DataSchema Utility: DataSchema.JSON

DataSchema.JSON normalizes arbitrary JSON data against a given schema into an object with known properties.

Basic example

Data
{
    "total":10,
    "results":[
        {"n":1, "fname":"George", "lname":"Washington"},
        {"n":2, "fname":"John", "lname":"Adams"},
        {"n":3, "fname":"Thomas", "lname":"Jefferson"},
        {"n":4, "fname":"James", "lname":"Madison"},
        {"n":5, "fname":"James", "lname":"Monroe"},
        {"n":6, "fname":"John", "mname":"Quincy", "lname":"Adams"},
        {"n":7, "fname":"Andrew", "lname":"Jackson"},
        {"n":8, "fname":"Martin", "lname":"Van Buren"},
        {"n":9, "fname":"William", "mname":"Henry", "lname":"Harrison"},
        {"n":10, "fname":"John", "lname":"Tyler"}
    ]
}
    
Schema
{
    metaFields: {total:"total"},
    resultListLocator: "results",
    resultFields: [{key:"n"}, {key:"fname"}, {key:"lname"}]
}
    
Normalized data

Complex example

Data
{
    "profile":{
        "current":160,
        "target":150
    },
    "reference": [
        {
            "category":"exercise",
            "type":"expenditure",
            "activities":[
                {"name":"biking", "calories":550},
                {"name":"golf", "calories":1000},
                {"name":"running", "calories":650},
                {"name":"swimming", "calories":650},
                {"name":"walking", "calories":225}
            ]
        },
        {
            "category":"nutrition",
            "type":"intake",
            "fruit":[
                {"name":"apple", "calories":70},
                {"name":"banana", "calories":70},
                {"name":"orange", "calories":90},
            ],
            "vegetables":[
                {"name":"baked potato", "calories":150},
                {"name":"broccoli", "calories":50},
                {"name":"green beans", "calories":30}
            ]
        }
    ],
    "program": [
        {
            "category":"exercise",
            "schedule":[
                {"day":"sunday", "activity":"swimming"},
                {"day":"monday", "activity":"running"},
                {"day":"tuesday", "activity":"biking"},
                {"day":"wednesday", "activity":"running"},
                {"day":"thursday", "activity":"swimming"},
                {"day":"friday", "activity":"running"},
                {"day":"saturday", "activity":"golf"}
            ]
        },
        {
            "category":"diet",
            "schedule":[
            ]
        }
    ]
}
    
Schema
{
    metaFields: {current:"profile.current", target:"profile.target",
        reference:"reference[0].activities"},
    resultListLocator: "program[0].schedule",
    resultFields: [{key:"day"}, {key:"activity"}]
}
    
Normalized data

In order to use DataSchema.JSON, input data must be a JavaScript object.

  1. YUI().use("dataschema-json", function(Y) {
  2. var data_in = {
  3. total:10,
  4. results:[
  5. {n:1, fname:"George", lname:"Washington"},
  6. {n:2, fname:"John", lname:"Adams"},
  7. {n:3, fname:"Thomas", lname:"Jefferson"},
  8. {n:4, fname:"James", lname:"Madison"},
  9. {n:5, fname:"James", lname:"Monroe"},
  10. {n:6, fname:"John", mname:"Quincy", lname:"Adams"},
  11. {n:7, fname:"Andrew", lname:"Jackson"},
  12. {n:8, fname:"Martin", lname:"Van Buren"},
  13. {n:9, fname:"William", mName:"Henry", lname:"Harrison"},
  14. {n:10, fname:"John", lname:"Tyler"}
  15. ]
  16. },
  17. schema = {
  18. metaFields: {total:"total"},
  19. resultListLocator: "results",
  20. // Or simply: ["n", "fname", "lname"]
  21. resultFields: [{key:"n"}, {key:"fname"}, {key:"lname"}]
  22. },
  23. data_out = Y.DataSchema.JSON.apply(schema, data_in));
  24.  
  25. alert(data_out);
  26. });
YUI().use("dataschema-json", function(Y) {
    var data_in = {
            total:10,
            results:[
                {n:1, fname:"George", lname:"Washington"},
                {n:2, fname:"John", lname:"Adams"},
                {n:3, fname:"Thomas", lname:"Jefferson"},
                {n:4, fname:"James", lname:"Madison"},
                {n:5, fname:"James", lname:"Monroe"},
                {n:6, fname:"John", mname:"Quincy", lname:"Adams"},
                {n:7, fname:"Andrew", lname:"Jackson"},
                {n:8, fname:"Martin", lname:"Van Buren"},
                {n:9, fname:"William", mName:"Henry", lname:"Harrison"},
                {n:10, fname:"John", lname:"Tyler"}
            ]
        },
        schema = {
            metaFields: {total:"total"},
            resultListLocator: "results",
            // Or simply: ["n", "fname", "lname"]
            resultFields: [{key:"n"}, {key:"fname"}, {key:"lname"}]
        },
        data_out = Y.DataSchema.JSON.apply(schema, data_in));
 
    alert(data_out);
});

The data itself can get fairly complex, with deeply nested arrays and objects. In your schema, you can use dot notation and the array-index syntax to define these locations. When necessary, you can also use object-bracket notation to define locations that might otherwise be invalid with dot notation.

  1. YUI().use("dataschema-json", function(Y) {
  2. var data_in = {
  3. "profile":{
  4. "current":160,
  5. "target":150
  6. },
  7. "reference": [
  8. {
  9. "category":"exercise",
  10. "type":"expenditure",
  11. "activities":[
  12. {"name":"biking", "calories":550},
  13. {"name":"golf", "calories":1000},
  14. {"name":"running", "calories":650},
  15. {"name":"swimming", "calories":650},
  16. {"name":"walking", "calories":225}
  17. ]
  18. },
  19. {
  20. "category":"nutrition",
  21. "type":"intake",
  22. "fruit":[
  23. {"name":"apple", "calories":70},
  24. {"name":"banana", "calories":70},
  25. {"name":"orange", "calories":90},
  26. ],
  27. "vegetables":[
  28. {"name":"baked potato", "calories":150},
  29. {"name":"broccoli", "calories":50},
  30. {"name":"green beans", "calories":30}
  31. ]
  32. }
  33. ],
  34. "program": [
  35. {
  36. "category":"exercise",
  37. "weekly schedule":[
  38. {"day":"sunday", "activity":"swimming"},
  39. {"day":"monday", "activity":"running"},
  40. {"day":"tuesday", "activity":"biking"},
  41. {"day":"wednesday", "activity":"running"},
  42. {"day":"thursday", "activity":"swimming"},
  43. {"day":"friday", "activity":"running"},
  44. {"day":"saturday", "activity":"golf"}
  45. ]
  46. },
  47. {
  48. "category":"diet",
  49. "schedule":[
  50. ]
  51. }
  52. ]
  53. },
  54. schema = {
  55. metaFields: {current:"profile.current", target:"profile.target",
  56. reference:"reference[0].activities"},
  57. resultListLocator: "program[0]['weekly schedule']",
  58. // Or simply: ["day", "activity"]
  59. resultFields: [{key:"day"}, {key:"activity"}]
  60. },
  61. data_out = Y.DataSchema.Array.apply(schema, data_in));
  62.  
  63. alert(data_out);
  64. });
YUI().use("dataschema-json", function(Y) {
        var data_in = {
            "profile":{
                "current":160,
                "target":150
            },
            "reference": [
                {
                    "category":"exercise",
                    "type":"expenditure",
                    "activities":[
                        {"name":"biking", "calories":550},
                        {"name":"golf", "calories":1000},
                        {"name":"running", "calories":650},
                        {"name":"swimming", "calories":650},
                        {"name":"walking", "calories":225}
                    ]
                },
                {
                    "category":"nutrition",
                    "type":"intake",
                    "fruit":[
                        {"name":"apple", "calories":70},
                        {"name":"banana", "calories":70},
                        {"name":"orange", "calories":90},
                    ],
                    "vegetables":[
                        {"name":"baked potato", "calories":150},
                        {"name":"broccoli", "calories":50},
                        {"name":"green beans", "calories":30}
                    ]
                }
            ],
            "program": [
                {
                    "category":"exercise",
                    "weekly schedule":[
                        {"day":"sunday", "activity":"swimming"},
                        {"day":"monday", "activity":"running"},
                        {"day":"tuesday", "activity":"biking"},
                        {"day":"wednesday", "activity":"running"},
                        {"day":"thursday", "activity":"swimming"},
                        {"day":"friday", "activity":"running"},
                        {"day":"saturday", "activity":"golf"}
                    ]
                },
                {
                    "category":"diet",
                    "schedule":[
                    ]
                }
            ]
        },
        schema = {
            metaFields: {current:"profile.current", target:"profile.target",
                reference:"reference[0].activities"},
            resultListLocator: "program[0]['weekly schedule']",
            // Or simply: ["day", "activity"]
            resultFields: [{key:"day"}, {key:"activity"}]
        },
        data_out = Y.DataSchema.Array.apply(schema, data_in));
 
    alert(data_out);
});

Copyright © 2009 Yahoo! Inc. All rights reserved.

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