Skip to content

Instantly share code, notes, and snippets.

@hbi99
Last active January 1, 2016 06:19
Show Gist options
  • Save hbi99/8104182 to your computer and use it in GitHub Desktop.
Save hbi99/8104182 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