UNPKG

5 kBJavaScriptView Raw
1var expect = require('expect.js'),
2util = require('util'),
3jpath = require('..')
4;
5
6// From: http://goessner.net/articles/JsonPath/
7var data = {
8 store: {
9 book: [
10 { category: "reference",
11 author: "Nigel Rees",
12 title: "Sayings of the Century",
13 price: 8.95
14 },
15 { category: "fiction",
16 author: "Evelyn Waugh",
17 title: "Sword of Honour",
18 price: 12.99
19 },
20 { category: "fiction",
21 author: "Herman Melville",
22 title: "Moby Dick",
23 isbn: "0-553-21311-3",
24 price: 8.99
25 },
26 { category: "fiction",
27 author: "J. R. R. Tolkien",
28 title: "The Lord of the Rings",
29 isbn: "0-395-19395-8",
30 price: 22.99
31 }
32 ],
33 bicycle: {
34 color: "red",
35 price: 19.95
36 }
37 }
38};
39
40// jpath preamble is $
41// -- path segments are interpreted the same as JSON Pointer,
42// with selector instructions in square brackets [].
43
44// select the root object:
45var p = jpath.create("#");
46var res = p.resolve(data);
47expect(res).to.contain(data);
48
49
50// select the authors of all books:
51p = jpath.parseSelector("#/store/book[*][#/author]");
52res = jpath.executeSelectors(data, p);
53expect(res).to.contain('Evelyn Waugh');
54expect(res).to.contain('Nigel Rees');
55expect(res).to.contain('Herman Melville');
56expect(res).to.contain('J. R. R. Tolkien');
57
58// select all authors:
59p = jpath.parseSelector("[..#/author]");
60res = jpath.executeSelectors(data, p);
61expect(res).to.contain('Evelyn Waugh');
62expect(res).to.contain('Nigel Rees');
63expect(res).to.contain('Herman Melville');
64expect(res).to.contain('J. R. R. Tolkien');
65
66// select all things in store
67p = jpath.parseSelector("#/store[*]");
68res = jpath.executeSelectors(data, p);
69expect(res).to.contain(data.store.book);
70expect(res).to.contain(data.store.bicycle);
71
72// resolved.should.eql(data);
73
74// select the price of everything in the store
75p = jpath.parseSelector("#/store[..#/price]");
76res = jpath.executeSelectors(data, p);
77expect(res).to.contain(8.95);
78expect(res).to.contain(12.99);
79expect(res).to.contain(8.99);
80expect(res).to.contain(22.99);
81expect(res).to.contain(19.95);
82
83
84// select the third book
85p = jpath.parseSelector("[..#/book/2]");
86res = jpath.executeSelectors(data, p);
87expect(res).to.contain(data.store.book[2]);
88
89p = jpath.parseSelector("[..#/book[2]]");
90res = jpath.executeSelectors(data, p);
91expect(res).to.contain(data.store.book[2]);
92
93// select the last book
94p = jpath.parseSelector("[..#/book[last]]");
95res = jpath.executeSelectors(data, p);
96expect(res).to.contain(data.store.book[3]);
97
98// select the first two books
99p = jpath.parseSelector("[..#/book[0,1]]");
100res = jpath.executeSelectors(data, p);
101expect(res).to.contain(data.store.book[0]);
102expect(res).to.contain(data.store.book[1]);
103
104// select books without an isbn
105p = jpath.parseSelector("[..#/book[*][#/isbn]]");
106res = jpath.executeSelectors(data, p);
107
108// select author and title from any book
109p = jpath.parseSelector("..#/book[*][take(/author,/title)]");
110res = jpath.executeSelectors(data, p);
111expect(res[0]).to.eql({
112 author: "Nigel Rees",
113 title: "Sayings of the Century"
114});
115expect(res[1]).to.eql({
116 author: "Evelyn Waugh",
117 title: "Sword of Honour"
118});
119expect(res[2]).to.eql({
120 author: "Herman Melville",
121 title: "Moby Dick"
122});
123expect(res[3]).to.eql({
124 author: "J. R. R. Tolkien",
125 title: "The Lord of the Rings"
126});
127
128// select title and price as cost from any book
129p = jpath.parseSelector("..#/book[*][take(/title,cost=/price)]");
130res = jpath.executeSelectors(data, p);
131expect(res[0]).to.eql({
132 title: "Sayings of the Century",
133 cost: 8.95
134});
135expect(res[1]).to.eql({
136 title: "Sword of Honour",
137 cost: 12.99
138});
139expect(res[2]).to.eql({
140 title: "Moby Dick",
141 cost: 8.99
142});
143expect(res[3]).to.eql({
144 title: "The Lord of the Rings",
145 cost: 22.99
146});
147
148// select books priced more than 10 via a selector fn
149// p = jpath.parseSelector("[..#/book[*][!{#/price < 10 || !exists(#/author)}][@myfn]]");
150p = jpath.parseSelector("[..#/book[*][@myfn]]");
151res = jpath.executeSelectors(data, p, {
152 myfn: function(obj, accum, sel) {
153 if (obj.price && obj.price < 10)
154 accum.push(obj);
155 return accum;
156 }
157});
158expect(res).to.contain(data.store.book[0]);
159expect(res).to.contain(data.store.book[2]);
160
161p = jpath.create("[..#/book[*][@myfn][#/category]]");
162res = p.resolve(data, {
163 myfn: function(obj, accum, sel) {
164 if (obj.price && obj.price < 10)
165 accum.push(obj);
166 return accum;
167 }
168});
169expect(res).to.contain(data.store.book[0].category);
170expect(res).to.contain(data.store.book[2].category);
171
172p = jpath.resolve(data, '/store/book[first(2)]');
173
174p = jpath.create("#/store/book[*][@]");
175var res = p.resolve(data, function(obj, accum, sel) {
176 if (obj.price && obj.price < 10)
177 accum.push(obj);
178 return accum;
179});
180expect(res).to.contain(data["store"]["book"][0]);
181expect(res).to.contain(data["store"]["book"][2]);
182expect(res).to.have.length(2);
183
184// p = jpath.parseSelector("[..#/book[*][{#/price >= 10}]]");