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