Skip to content

Instantly share code, notes, and snippets.

@iwek
Created October 20, 2012 21:43
Show Gist options
  • Save iwek/3924925 to your computer and use it in GitHub Desktop.
Save iwek/3924925 to your computer and use it in GitHub Desktop.
Searching through JSON
//return an array of objects according to key, value, or key and value matching
function getObjects(obj, key, val) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjects(obj[i], key, val));
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (i == key && obj[i] == val || i == key && val == '') { //
objects.push(obj);
} else if (obj[i] == val && key == ''){
//only add if the object is not already in the array
if (objects.lastIndexOf(obj) == -1){
objects.push(obj);
}
}
}
return objects;
}
//return an array of values that match on a certain key
function getValues(obj, key) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getValues(obj[i], key));
} else if (i == key) {
objects.push(obj[i]);
}
}
return objects;
}
//return an array of keys that match on a certain value
function getKeys(obj, val) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getKeys(obj[i], val));
} else if (obj[i] == val) {
objects.push(i);
}
}
return objects;
}
var json = '{"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","ID":"44","str":"SGML","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}';
var js = JSON.parse(json);
//example of grabbing objects that match some key and value in JSON
console.log(getObjects(js,'ID','SGML'));
//returns 1 object where a key names ID has the value SGML
//example of grabbing objects that match some key in JSON
console.log(getObjects(js,'ID',''));
//returns 2 objects since keys with name ID are found in 2 objects
//example of grabbing obejcts that match some value in JSON
console.log(getObjects(js,'','SGML'));
//returns 2 object since 2 obects have keys with the value SGML
//example of grabbing objects that match some key in JSON
console.log(getObjects(js,'ID',''));
//returns 2 objects since keys with name ID are found in 2 objects
//example of grabbing values from any key passed in JSON
console.log(getValues(js,'ID'));
//returns array ["SGML", "44"]
//example of grabbing keys by searching via values in JSON
console.log(getKeys(js,'SGML'));
//returns array ["ID", "SortAs", "Acronym", "str"]
@ronnieserr
Copy link

Hi iwek,

The find-in-json gist is great! Could you extend it to an array of keys or values (rather than a single one)?

Thanks,

Ronnie Serr
ronnie@alpahbethouse.com

@sridharrao
Copy link

Hi iwek,

Its interesting. However, is there a way I can give search based on regular expression?

Thanks & Regards,
-Sridhar

@d3netxer
Copy link

d3netxer commented Feb 7, 2015

iwek, this code is amazing. Thank you!

@jmalmin
Copy link

jmalmin commented May 2, 2016

Thank you.. I needed to have the very clear directions you provided. Very nice of you to help others.

@SiriusBlackFi
Copy link

Thank you very much~That's really helpful.

@hijaren
Copy link

hijaren commented Jul 26, 2018

Thank you!

@Zerumi
Copy link

Zerumi commented Sep 15, 2019

Thanks a lot

@sarvadarshitarang
Copy link

i am running into a situation here like this:

  1. I have lots of json files with key value pairs with news type items
  2. i get a search query for which i need to fetch right key - value pair so that i can show that news
  3. can your script help there?

@mdrijwan
Copy link

Hi,

this is excellent. could you extend it to an array of keys or values (rather than a single one)? that'd be really helpful. thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment