Skip to content

Commit 24a1fa5

Browse files
author
Subbu Allamaraju
committed
1 parent 8a214f8 commit 24a1fa5

File tree

5 files changed

+108
-18
lines changed

5 files changed

+108
-18
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1+
*.iml
2+
.DS_Store
3+
.idea
4+
temp
15
node_modules
6+
pids
7+
reports
8+
target
9+
*.log

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
test

lib/jsonpath.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
* Licensed under the MIT (MIT-LICENSE.txt) licence.
55
*/
66

7+
var _ = require('underscore');
78
exports.eval = jsonPath;
89
var cache = {};
910
function jsonPath(obj, expr, arg) {
1011
var P = {
1112
resultType: arg && arg.resultType || "VALUE",
13+
flatten: arg && arg.flatten || false,
1214
result: [],
1315
normalize: function(expr) {
1416
if(cache[expr]) {
@@ -31,7 +33,19 @@ function jsonPath(obj, expr, arg) {
3133
return p;
3234
},
3335
store: function(p, v) {
34-
if (p) P.result[P.result.length] = P.resultType == "PATH" ? P.asPath(p) : v;
36+
if (p) {
37+
if (P.resultType == "PATH") {
38+
P.result[P.result.length] = P.asPath(p);
39+
}
40+
else {
41+
if(_.isArray(v) && P.flatten) {
42+
P.result = P.result.concat(v);
43+
}
44+
else {
45+
P.result[P.result.length] = v;
46+
}
47+
}
48+
}
3549
return !!p;
3650
},
3751
trace: function(expr, val, path) {

package.json

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
{
2-
"author": "Stefan Goessner",
3-
"name": "JSONPath",
4-
"description": "A JS implementation of JSONPath",
5-
"contributors": [
6-
{ "name": "Stefan Goessner", "email": "[email protected]" },
7-
{ "name": "Mike Brevoort", "email": "[email protected]" }
8-
],
9-
"version": "0.8.2",
10-
"repository": {
11-
"type": "git",
12-
"url": "git://github.com/s3u/JSONPath.git"
13-
},
14-
"main" : "./lib/jsonpath",
15-
"dependencies": {},
16-
"devDependencies": {
17-
"nodeunit": "latest"
18-
}
2+
"author": "Stefan Goessner",
3+
"name": "JSONPath",
4+
"description": "A JS implementation of JSONPath",
5+
"contributors": [
6+
{
7+
"name": "Prof. Gšssner",
8+
"email": "[email protected]"
9+
},
10+
{
11+
"name": "Subbu Allamaraju",
12+
"email": "[email protected]"
13+
},
14+
{
15+
"name": "Mike Brevoort",
16+
"email": "[email protected]"
17+
}
18+
],
19+
"version": "0.8.3",
20+
"repository": {
21+
"type": "git",
22+
"url": "git://github.com/s3u/JSONPath.git"
23+
},
24+
"main": "./lib/jsonpath",
25+
"dependencies": {
26+
"underscore": "latest"
27+
},
28+
"devDependencies": {
29+
"nodeunit": "latest"
30+
}
1931
}

test/test.intermixed.arr.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var jsonpath = require("../").eval,
2+
sys = require('sys'),
3+
testCase = require('nodeunit').testCase
4+
5+
// tests based on examples at http://goessner.net/articles/JsonPath/
6+
7+
var json = {"store":{
8+
"book":[
9+
{ "category":"reference",
10+
"author":"Nigel Rees",
11+
"title":"Sayings of the Century",
12+
"price":[8.95, 8.94, 8.93]
13+
},
14+
{ "category":"fiction",
15+
"author":"Evelyn Waugh",
16+
"title":"Sword of Honour",
17+
"price":12.99
18+
},
19+
{ "category":"fiction",
20+
"author":"Herman Melville",
21+
"title":"Moby Dick",
22+
"isbn":"0-553-21311-3",
23+
"price":8.99
24+
},
25+
{ "category":"fiction",
26+
"author":"J. R. R. Tolkien",
27+
"title":"The Lord of the Rings",
28+
"isbn":"0-395-19395-8",
29+
"price":22.99
30+
}
31+
],
32+
"bicycle":{
33+
"color":"red",
34+
"price":19.95
35+
}
36+
}
37+
};
38+
39+
40+
module.exports = testCase({
41+
42+
// ============================================================================
43+
"all sub properties, entire tree":function (test) {
44+
// ============================================================================
45+
test.expect(1);
46+
var books = json.store.book;
47+
var expected = [books[1].price, books[2].price, books[3].price, json.store.bicycle.price];
48+
expected = books[0].price.concat(expected);
49+
var result = jsonpath(json, "$.store..price", {flatten: true});
50+
test.deepEqual(expected, result);
51+
52+
test.done();
53+
}
54+
});

0 commit comments

Comments
 (0)