Skip to content

Instantly share code, notes, and snippets.

@riverspirit
Last active December 21, 2015 06:59
Show Gist options
  • Save riverspirit/6268487 to your computer and use it in GitHub Desktop.
Save riverspirit/6268487 to your computer and use it in GitHub Desktop.
Parse nested json data and retrieve value of the given key.
var jsondata = {
'name': {
'fname': 'Jack',
'lname': [
{'familyName': 'Sparrow'},
{'surname': 'Captain'},
{'tags':[
{'test': 'mocha'},
'program',
{'level': 'five',}
]}
]
}
};
var result = get_value_nested(jsondata, 'test');
console.log(result);
function get_value_nested(dataObject, keyName) {
var value = null;
this.get_value = function (dataObject, keyName) {
for (i in dataObject) {
if (i == keyName) {
value = dataObject[keyName];
} else if (typeof dataObject[i] == 'object') {
this.get_value(dataObject[i], keyName);
}
}
return value;
}
this.get_value(dataObject, keyName);
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment