root / trunk / web / dojo / dojox / jsonPath / README @ 10
History | View | Annotate | Download (6.45 KB)
1 | 9 | andrej.cim | ------------------------------------------------------------------------------- |
---|---|---|---|
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 | ------------------------------------------------------------------------------- |
||
21 | Dependencies: |
||
22 | |||
23 | Dojo Core (package loader). |
||
24 | ------------------------------------------------------------------------------- |
||
25 | Documentation |
||
26 | |||
27 | Usage: |
||
28 | |||
29 | var matches = dojox.jsonPath.query(objectToQuery, jsonPathExpresson) |
||
30 | |||
31 | Expressions: |
||
32 | |||
33 | $ The Root Object |
||
34 | @ The current object/element |
||
35 | . or [] The child operator |
||
36 | .. Recursive descent |
||
37 | * all objects |
||
38 | [] subscript operator |
||
39 | [,] Union operator |
||
40 | [start:end:step] array slice operator |
||
41 | ?() applies a filter/script expression |
||
42 | () script expresions |
||
43 | |||
44 | some examples: |
||
45 | |||
46 | Given the following test data set: |
||
47 | |||
48 | var json = |
||
49 | { "store": { |
||
50 | "book": [ |
||
51 | { "category": "reference", |
||
52 | "author": "Nigel Rees", |
||
53 | "title": "Sayings of the Century", |
||
54 | "price": 8.95 |
||
55 | }, |
||
56 | { "category": "fiction", |
||
57 | "author": "Evelyn Waugh", |
||
58 | "title": "Sword of Honour", |
||
59 | "price": 12.99 |
||
60 | }, |
||
61 | { "category": "fiction", |
||
62 | "author": "Herman Melville", |
||
63 | "title": "Moby Dick", |
||
64 | "isbn": "0-553-21311-3", |
||
65 | "price": 8.99 |
||
66 | }, |
||
67 | { "category": "fiction", |
||
68 | "author": "J. R. R. Tolkien", |
||
69 | "title": "The Lord of the Rings", |
||
70 | "isbn": "0-395-19395-8", |
||
71 | "price": 22.99 |
||
72 | } |
||
73 | ], |
||
74 | "bicycle": { |
||
75 | "color": "red", |
||
76 | "price": 19.95 |
||
77 | } |
||
78 | } |
||
79 | }; |
||
80 | |||
81 | Here are some example queries and their output: |
||
82 | |||
83 | $.store.book[*].author |
||
84 | ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"] |
||
85 | |||
86 | $..author |
||
87 | ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"] |
||
88 | |||
89 | $.store.* |
||
90 | [[{"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}] |
||
91 | |||
92 | $.store..price |
||
93 | [8.95,12.99,8.99,22.99,19.95] |
||
94 | |||
95 | $..book[(@.length-1)] |
||
96 | [{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}] |
||
97 | |||
98 | $..book[-1] |
||
99 | [{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}] |
||
100 | |||
101 | $..book[0,1] |
||
102 | [{"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}] |
||
103 | |||
104 | $..book[:2] |
||
105 | [{"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}] |
||
106 | |||
107 | $..book[?(@.isbn)] |
||
108 | [{"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}] |
||
109 | |||
110 | $..book[?(@.price<10)] |
||
111 | [{"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}] |
||
112 | |||
113 | $..* |
||
114 | [{"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] |
||
115 | |||
116 | |||
117 | ------------------------------------------------------------------------------- |
||
118 | Installation instructions |
||
119 | |||
120 | Grab the following from the Dojo SVN Repository: |
||
121 | http://svn.dojotoolkit.org/var/src/dojo/dojox/trunk/jsonPath |
||
122 | |||
123 | Install into the following directory structure: |
||
124 | /dojox/jsonPath/ |
||
125 | |||
126 | ...which should be at the same level as your Dojo checkout. |