| 1 | | ------------------------------------------------------------------------------- |
| 2 | | dojox.jsonPath |
| 3 | | ------------------------------------------------------------------------------- |
| 4 | | Version 1.0 |
| 5 | | Release date: 11/14/2007 |
| 6 | | ------------------------------------------------------------------------------- |
| 7 | | Project state: beta |
| 8 | | ------------------------------------------------------------------------------- |
| 9 | | Project authors |
| 10 | | Dustin Machi |
| 11 | | Kris Zyp |
| 12 | | ------------------------------------------------------------------------------- |
| 13 | | Project description |
| 14 | | |
| 15 | | jsonPath is a query system similar in idea to xpath, but for use against |
| 16 | | javascript objects. This code is a port of the jsonPath code at |
| 17 | | http://code.google.com/p/jsonpath/. It was contributed under CLA by Stefan |
| 18 | | Goessner. Thanks Stefan! |
| 19 | | ------------------------------------------------------------------------------- |
| 20 | | Dependencies: |
| 21 | | |
| 22 | | Dojo Core (package loader). |
| 23 | | ------------------------------------------------------------------------------- |
| 24 | | Documentation |
| 25 | | |
| 26 | | Usage: |
| 27 | | |
| 28 | | var matches = dojox.jsonPath.query(objectToQuery, jsonPathExpresson) |
| 29 | | |
| 30 | | Expressions: |
| 31 | | |
| 32 | | $ The Root Object |
| 33 | | @ The current object/element |
| 34 | | . or [] The child operator |
| 35 | | .. Recursive descent |
| 36 | | * all objects |
| 37 | | [] subscript operator |
| 38 | | [,] Union operator |
| 39 | | [start:end:step] array slice operator |
| 40 | | ?() applies a filter/script expression |
| 41 | | () script expresions |
| 42 | | |
| 43 | | some examples: |
| 44 | | |
| 45 | | Given the following test data set: |
| 46 | | |
| 47 | | var json = |
| 48 | | { "store": { |
| 49 | | "book": [ |
| 50 | | { "category": "reference", |
| 51 | | "author": "Nigel Rees", |
| 52 | | "title": "Sayings of the Century", |
| 53 | | "price": 8.95 |
| 54 | | }, |
| 55 | | { "category": "fiction", |
| 56 | | "author": "Evelyn Waugh", |
| 57 | | "title": "Sword of Honour", |
| 58 | | "price": 12.99 |
| 59 | | }, |
| 60 | | { "category": "fiction", |
| 61 | | "author": "Herman Melville", |
| 62 | | "title": "Moby Dick", |
| 63 | | "isbn": "0-553-21311-3", |
| 64 | | "price": 8.99 |
| 65 | | }, |
| 66 | | { "category": "fiction", |
| 67 | | "author": "J. R. R. Tolkien", |
| 68 | | "title": "The Lord of the Rings", |
| 69 | | "isbn": "0-395-19395-8", |
| 70 | | "price": 22.99 |
| 71 | | } |
| 72 | | ], |
| 73 | | "bicycle": { |
| 74 | | "color": "red", |
| 75 | | "price": 19.95 |
| 76 | | } |
| 77 | | } |
| 78 | | }; |
| 79 | | |
| 80 | | Here are some example queries and their output: |
| 81 | | |
| 82 | | $.store.book[*].author |
| 83 | | ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"] |
| 84 | | |
| 85 | | $..author |
| 86 | | ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"] |
| 87 | | |
| 88 | | $.store.* |
| 89 | | [[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],{"color":"red","price":19.95}] |
| 90 | | |
| 91 | | $.store..price |
| 92 | | [8.95,12.99,8.99,22.99,19.95] |
| 93 | | |
| 94 | | $..book[(@.length-1)] |
| 95 | | [{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}] |
| 96 | | |
| 97 | | $..book[-1] |
| 98 | | [{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}] |
| 99 | | |
| 100 | | $..book[0,1] |
| 101 | | [{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}] |
| 102 | | |
| 103 | | $..book[:2] |
| 104 | | [{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}] |
| 105 | | |
| 106 | | $..book[?(@.isbn)] |
| 107 | | [{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}] |
| 108 | | |
| 109 | | $..book[?(@.price<10)] |
| 110 | | [{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}] |
| 111 | | |
| 112 | | $..* |
| 113 | | [{"book":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],"bicycle":{"color":"red","price":19.95}},[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],{"color":"red","price":19.95},{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99},"reference","Nigel Rees","Sayings of the Century",8.95,"fiction","Evelyn Waugh","Sword of Honour",12.99,"fiction","Herman Melville","Moby Dick","0-553-21311-3",8.99,"fiction","J. R. R. Tolkien","The Lord of the Rings","0-395-19395-8",22.99,"red",19.95] |
| 114 | | |
| 115 | | |
| 116 | | ------------------------------------------------------------------------------- |
| 117 | | Installation instructions |
| 118 | | |
| 119 | | Grab the following from the Dojo SVN Repository: |
| 120 | | http://svn.dojotoolkit.org/var/src/dojo/dojox/trunk/jsonPath |
| 121 | | |
| 122 | | Install into the following directory structure: |
| 123 | | /dojox/jsonPath/ |
| 124 | | |
| 125 | | ...which should be at the same level as your Dojo checkout. |
| | 1 | ------------------------------------------------------------------------------- |
| | 2 | dojox.jsonPath |
| | 3 | ------------------------------------------------------------------------------- |
| | 4 | Version 1.0 |
| | 5 | Release date: 11/14/2007 |
| | 6 | ------------------------------------------------------------------------------- |
| | 7 | Project state: beta |
| | 8 | ------------------------------------------------------------------------------- |
| | 9 | Project authors |
| | 10 | Dustin Machi |
| | 11 | Kris Zyp |
| | 12 | ------------------------------------------------------------------------------- |
| | 13 | Project description |
| | 14 | |
| | 15 | jsonPath is a query system similar in idea to xpath, but for use against |
| | 16 | javascript objects. This code is a port of the jsonPath code at |
| | 17 | http://code.google.com/p/jsonpath/. It was contributed under CLA by Stefan |
| | 18 | Goessner. Thanks Stefan! |
| | 19 | ------------------------------------------------------------------------------- |
| | 20 | Dependencies: |
| | 21 | |
| | 22 | Dojo Core (package loader). |
| | 23 | ------------------------------------------------------------------------------- |
| | 24 | Documentation |
| | 25 | |
| | 26 | Usage: |
| | 27 | |
| | 28 | var matches = dojox.jsonPath.query(objectToQuery, jsonPathExpresson, options) |
| | 29 | |
| | 30 | jsonPath supports two forms of evaluation. Item-based evaluation, which is the |
| | 31 | default evaluation, executes each expression on each item in the result at each step in evaluation. |
| | 32 | Result-based evaluation executes each expression on the result at each step in evaluation, and |
| | 33 | can be selected by providing the third parameter, options with an evalType equal to "RESULT" like: |
| | 34 | jsonPath(object,query, {evalType:"RESULT"}); |
| | 35 | Item-based evaluation will always produce an array of results. Result-basd evaluation follows the |
| | 36 | semantics of normal JavaScript more closely. Selecting a single item from an array by index, will actually |
| | 37 | return that item rather than an array with that item in it. With result-based evaluation, you can also |
| | 38 | use the slice operator to narrow down search results, allowing you to request paged results of queries. |
| | 39 | |
| | 40 | |
| | 41 | Expressions: |
| | 42 | |
| | 43 | $ The Root Object |
| | 44 | @ The current object/element |
| | 45 | . or [] The child operator |
| | 46 | .. Recursive descent |
| | 47 | * all objects |
| | 48 | [] subscript operator |
| | 49 | [,] Union operator |
| | 50 | [start:end:step] array slice operator |
| | 51 | ?() applies a filter sub-expression to extract all items from an array that match the expression |
| | 52 | () sub-expression |
| | 53 | |
| | 54 | some examples: |
| | 55 | |
| | 56 | For portability, sub-expressions should be limited to the following operators: |
| | 57 | &, |, =, !=, >=, <=, +, -, /, *, ?, : |
| | 58 | |
| | 59 | You may use the safe-eval option {safeEval:true}, to restrict sub-expressions to these operators. This |
| | 60 | prevents the execution of arbitrary code, and is recommended for user supplied queries. Safe-eval |
| | 61 | also uses a subset of operators that ensures portability of the JSONPath expressions across all |
| | 62 | languages (without requiring a JavaScript interpreter). |
| | 63 | |
| | 64 | Given the following test data set: |
| | 65 | |
| | 66 | var json = |
| | 67 | { "store": { |
| | 68 | "book": [ |
| | 69 | { "category": "reference", |
| | 70 | "author": "Nigel Rees", |
| | 71 | "title": "Sayings of the Century", |
| | 72 | "price": 8.95 |
| | 73 | }, |
| | 74 | { "category": "fiction", |
| | 75 | "author": "Evelyn Waugh", |
| | 76 | "title": "Sword of Honour", |
| | 77 | "price": 12.99 |
| | 78 | }, |
| | 79 | { "category": "fiction", |
| | 80 | "author": "Herman Melville", |
| | 81 | "title": "Moby Dick", |
| | 82 | "isbn": "0-553-21311-3", |
| | 83 | "price": 8.99 |
| | 84 | }, |
| | 85 | { "category": "fiction", |
| | 86 | "author": "J. R. R. Tolkien", |
| | 87 | "title": "The Lord of the Rings", |
| | 88 | "isbn": "0-395-19395-8", |
| | 89 | "price": 22.99 |
| | 90 | } |
| | 91 | ], |
| | 92 | "bicycle": { |
| | 93 | "color": "red", |
| | 94 | "price": 19.95 |
| | 95 | } |
| | 96 | } |
| | 97 | }; |
| | 98 | |
| | 99 | Here are some example queries and their output (when result-based |
| | 100 | evaluation and item-based evaluation differ, it is noted): |
| | 101 | |
| | 102 | $.store.book[*].author |
| | 103 | ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"] |
| | 104 | |
| | 105 | $..author |
| | 106 | ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"] |
| | 107 | |
| | 108 | $.store.* |
| | 109 | [[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],{"color":"red","price":19.95}] |
| | 110 | |
| | 111 | $.store..price |
| | 112 | [8.95,12.99,8.99,22.99,19.95] |
| | 113 | |
| | 114 | $..book[(@.length-1)] |
| | 115 | item-based: [{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}] |
| | 116 | result-based: {"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99} |
| | 117 | |
| | 118 | $..book[-1] |
| | 119 | item-based: [{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}] |
| | 120 | result-based: {"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99} |
| | 121 | |
| | 122 | $..book[0,1] |
| | 123 | [{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}] |
| | 124 | |
| | 125 | $..book[:2] |
| | 126 | [{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}] |
| | 127 | |
| | 128 | $..book[?(@.isbn)] |
| | 129 | [{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}] |
| | 130 | |
| | 131 | $..book[?(@.price<10)] |
| | 132 | [{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}] |
| | 133 | |
| | 134 | $..* |
| | 135 | [{"book":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],"bicycle":{"color":"red","price":19.95}},[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],{"color":"red","price":19.95},{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99},"reference","Nigel Rees","Sayings of the Century",8.95,"fiction","Evelyn Waugh","Sword of Honour",12.99,"fiction","Herman Melville","Moby Dick","0-553-21311-3",8.99,"fiction","J. R. R. Tolkien","The Lord of the Rings","0-395-19395-8",22.99,"red",19.95] |
| | 136 | |
| | 137 | $..book[?(@.price<10)][0:2] |
| | 138 | item-based: [] |
| | 139 | result-based: [{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}] |
| | 140 | |
| | 141 | ------------------------------------------------------------------------------- |
| | 142 | Installation instructions |
| | 143 | |
| | 144 | Grab the following from the Dojo SVN Repository: |
| | 145 | http://svn.dojotoolkit.org/var/src/dojo/dojox/trunk/jsonPath |
| | 146 | |
| | 147 | Install into the following directory structure: |
| | 148 | /dojox/jsonPath/ |
| | 149 | |
| | 150 | ...which should be at the same level as your Dojo checkout. |