Skip to content

Instantly share code, notes, and snippets.

@hbi99
Last active August 29, 2015 14:14
Show Gist options
  • Save hbi99/a035c71cf6e34f47f9f8 to your computer and use it in GitHub Desktop.
Save hbi99/a035c71cf6e34f47f9f8 to your computer and use it in GitHub Desktop.
var obj = {
"car": [
{"id": 10, "color": "silver", "name": "Volvo"},
{"id": 11, "color": "red", "name": "Saab"},
{"id": 12, "color": "red", "name": "Peugeot"},
{"id": 13, "color": "yellow", "name": "Porsche"}
],
"bike": [
{"id": 20, "color": "black", "name": "Cannondale"},
{"id": 21, "color": "red", "name": "Shimano"}
]
},
search = JSON.search(obj, '//car[color="yellow"]/name');
console.log( search );
// ["Porsche"]
var reds = JSON.search(obj, '//*[color="red"]');
for (var i=0; i<reds.length; i++) {
console.log( reds[i].name );
}
// Saab
// Peugeot
// Shimano
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment