Skip to content

Commit 195b319

Browse files
authored
The oroginal 0.8.5 from Stefan Goessner
From the Google Code Archive of JSON Path at: https://code.google.com/archive/p/jsonpath/
1 parent cc41b65 commit 195b319

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

jsonpath.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/* JSONPath 0.8.5 - XPath for JSON
2+
*
3+
* Copyright (c) 2007 Stefan Goessner (goessner.net)
4+
* Licensed under the MIT (MIT-LICENSE.txt) licence.
5+
*
6+
* Proposal of Chris Zyp goes into version 0.9.x
7+
* Issue 7 resolved
8+
*/
9+
function jsonPath(obj, expr, arg) {
10+
var P = {
11+
resultType: arg && arg.resultType || "VALUE",
12+
result: [],
13+
normalize: function(expr) {
14+
var subx = [];
15+
return expr.replace(/[\['](\??\(.*?\))[\]']|\['(.*?)'\]/g, function($0,$1,$2){return "[#"+(subx.push($1||$2)-1)+"]";}) /* http://code.google.com/p/jsonpath/issues/detail?id=4 */
16+
.replace(/'?\.'?|\['?/g, ";")
17+
.replace(/;;;|;;/g, ";..;")
18+
.replace(/;$|'?\]|'$/g, "")
19+
.replace(/#([0-9]+)/g, function($0,$1){return subx[$1];});
20+
},
21+
asPath: function(path) {
22+
var x = path.split(";"), p = "$";
23+
for (var i=1,n=x.length; i<n; i++)
24+
p += /^[0-9*]+$/.test(x[i]) ? ("["+x[i]+"]") : ("['"+x[i]+"']");
25+
return p;
26+
},
27+
store: function(p, v) {
28+
if (p) P.result[P.result.length] = P.resultType == "PATH" ? P.asPath(p) : v;
29+
return !!p;
30+
},
31+
trace: function(expr, val, path) {
32+
if (expr !== "") {
33+
var x = expr.split(";"), loc = x.shift();
34+
x = x.join(";");
35+
if (val && val.hasOwnProperty(loc))
36+
P.trace(x, val[loc], path + ";" + loc);
37+
else if (loc === "*")
38+
P.walk(loc, x, val, path, function(m,l,x,v,p) { P.trace(m+";"+x,v,p); });
39+
else if (loc === "..") {
40+
P.trace(x, val, path);
41+
P.walk(loc, x, val, path, function(m,l,x,v,p) { typeof v[m] === "object" && P.trace("..;"+x,v[m],p+";"+m); });
42+
}
43+
else if (/^\(.*?\)$/.test(loc)) /* [(expr)] */
44+
P.trace(P.eval(loc, val, path.substr(path.lastIndexOf(";")+1))+";"+x, val, path);
45+
else if (/^\?\(.*?\)$/.test(loc)) /* [?(expr)] */
46+
P.walk(loc, x, val, path, function(m,l,x,v,p) { if (P.eval(l.replace(/^\?\((.*?)\)$/,"$1"), v instanceof Array ? v[m] : v, m)) P.trace(m+";"+x,v,p); }); /* issue 5 resolved */
47+
else if (/^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$/.test(loc)) / [start:end:step] /* python slice syntax */
48+
P.slice(loc, x, val, path);
49+
else if (/,/.test(loc)) { /* [name1,name2,...] */
50+
for (var s=loc.split(/'?,'?/),i=0,n=s.length; i<n; i++)
51+
P.trace(s[i]+";"+x, val, path);
52+
}
53+
}
54+
else
55+
P.store(path, val);
56+
},
57+
walk: function(loc, expr, val, path, f) {
58+
if (val instanceof Array) {
59+
for (var i=0,n=val.length; i<n; i++)
60+
if (i in val)
61+
f(i,loc,expr,val,path);
62+
}
63+
else if (typeof val === "object") {
64+
for (var m in val)
65+
if (val.hasOwnProperty(m))
66+
f(m,loc,expr,val,path);
67+
}
68+
},
69+
slice: function(loc, expr, val, path) {
70+
if (val instanceof Array) {
71+
var len=val.length, start=0, end=len, step=1;
72+
loc.replace(/^(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)$/g, function($0,$1,$2,$3){start=parseInt($1||start);end=parseInt($2||end);step=parseInt($3||step);});
73+
start = (start < 0) ? Math.max(0,start+len) : Math.min(len,start);
74+
end = (end < 0) ? Math.max(0,end+len) : Math.min(len,end);
75+
for (var i=start; i<end; i+=step)
76+
P.trace(i+";"+expr, val, path);
77+
}
78+
},
79+
eval: function(x, _v, _vname) {
80+
try { return $ && _v && eval(x.replace(/(^|[^\\])@/g, "$1_v").replace(/\\@/g, "@")); } /* issue 7 : resolved */
81+
catch(e) { throw new SyntaxError("jsonPath: " + e.message + ": " + x.replace(/(^|[^\\])@/g, "$1_v").replace(/\\@/g, "@")); } /* issue 7 : resolved */
82+
}
83+
};
84+
85+
var $ = obj;
86+
if (expr && obj && (P.resultType == "VALUE" || P.resultType == "PATH")) {
87+
P.trace(P.normalize(expr).replace(/^\$;?/,""), obj, "$"); /* issue 6 resolved */
88+
return P.result.length ? P.result : false;
89+
}
90+
}

0 commit comments

Comments
 (0)