For background, please refer to this question: Access deep object member of embeded JSON
The solutions offered there worked very well with the packed JSON contained in key values.
However, they don't handle the situation where JSON has arrays.
The original function I referenced in the other question DID handle arrays, but it would not handle the packed JSON.
This is the original function:
function getPathValue(obj, path) {
return new Function('_', 'return _.' + path)(obj);
}
and this is the answer from the first question:
function getValue(object, path) {
return path
.split('.')
.reduce((o, k) => (typeof o === 'string' ? JSON.parse(o) : o)[k],
object);
}
Again, both work well, but neither offers the whole package.
I need a solution that will do both and it must work in IE11's ES5.
Here is a sample API returned JSON string:
{"id":"0001","type":"donut","name":"Cake","ppu":0.55,"batters":{"batter":[{"id":"1001","type":"Regular"},{"id":"1002","type":"Chocolate"}]},"data":"{\"domain\":\"cooking.com\",\"id\":53819390}"}
I'd like to be able to query values with a path string, for example:
value = getPathValue(obj, 'batters.batter[2].id');
or
value = getPathValue(obj, 'type');
or
value = getPathValue(obj, 'data.domain');
The following would do the job using a Regex on each value :
You could replace the brackets and take the remaining values as keys. Inside of the reducing, you could use a default object for not given objects.