Last active
August 29, 2015 14:15
-
-
Save mikegwhit/895dfd7a551d804e5f7c to your computer and use it in GitHub Desktop.
performs a JSON search using regex... goal is to return the associated keys, values, and parent key chain for any search result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JSON.search=function(o,str){ | |
oobj = o; | |
o = JSON.stringify(o); | |
idx = o.search(str) | |
if ( idx == -1 ) | |
return []; | |
ocpy = o; | |
starts = [idx]; | |
while ( idx != -1 ) { | |
ocpy = ocpy.substr(idx+1); | |
idx = ocpy.search(str); | |
if ( idx == -1 ) | |
break; | |
starts.push(starts[starts.length-1]+idx+1); | |
} | |
keys = []; | |
backtrack = function(str,start,ignore){ | |
if ( ignore == undefined ) | |
ignore = false; | |
strsub = str.substr(0,start); | |
rquote = str.substr(start).search(/([0-9]|\{|\[|")\:?/); | |
quote = strsub.split("").reverse().join("").search(/"[^\\]?/); | |
if ( !ignore && str.substr(start).substr(rquote+1,1) == ":" ) { | |
return [str.substr(start-quote,quote+rquote),str.substr(0,start+rquote+1).match(new RegExp('\"'+str.substr(start-quote,quote+rquote)+'\"',"g")).length-1]; | |
} | |
strsub = strsub.split("").reverse().join("") | |
end = strsub.search(/([0-9]|\{|\[|")\:"/); | |
if ( end == -1 ) | |
return -1; | |
strsubsub = strsub.substr(end+3); | |
newstart = strsubsub.search(/"[^\\]?/); | |
key = strsub.substr(end+3,newstart); | |
key = key.split("").reverse().join(""); | |
num = str.substr(0,start-end).match(new RegExp('\"'+key+'\"',"g")).length-1; | |
return [key,num,start-end-newstart,start-end]; | |
} | |
keysearch = function(root,key,idx,matches){ | |
return (function(results,count,matches,idx){ | |
if ( matches == undefined ) | |
matches = [0,false]; | |
results = {}; | |
count = 0; | |
val = ""; | |
for ( var i in root ){ | |
if ( i == key && matches[0] == idx ) { | |
obj = {}; | |
obj[key] = root[i]; | |
return [obj,[1,true],obj[key]]; | |
} | |
if ( i == key ) | |
matches[0]++; | |
if (typeof root[i] != "object" || root[i] == null ) | |
continue; | |
count++; | |
result = (function(){return keysearch(root[i],key,idx,matches);})(); | |
matches[0] = result[1][0]; | |
matches[1] = result[1][1]; | |
found = matches[1]; | |
if ( found && result.length > 2 ) | |
val = result[2]; | |
result = result[0]; | |
if ( found ) { | |
results[i] = result; | |
break; | |
} | |
} | |
if ( count == 0 ) | |
return [[],matches,val]; | |
else | |
return [results,matches,val]; | |
})({},0,matches,idx); | |
} | |
for ( var i in starts ) { | |
key = backtrack(o,starts[i]); | |
parents = keysearch(oobj,key[0],key[1]); | |
obj = [key[0],parents[2],parents[0]]; | |
keys.push(obj); | |
} | |
return keys; | |
} |
parent chain is now included
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
code currently is successful for searching values; is unsuccessful if a key is matched
need to include parent key chain in result