var jData = {
"value1": 5.0,
"value2": 2.5,
"value3": "2019-10-24T15:26:00.000Z",
"modifier": [],
"value4": {
"value41": {
"value411": 5,
"value412": "hey"
}
}
};
If You try with regex then use this:
JSON.stringify(jData).match(/(?<=(\"(value411|value412)\"\:))[\"\w+]+/g)
// Output: ['5', '"hey"']
Demo: https://regex101.com/r/c3K4cH/1
Limitation: You have to put only the key name, don't try to fetch the full object
You create a javascript function get any non-empty value from a JSON, as follows:
function getData(obj, fKey, fData = null) {
for (const prop of Object.getOwnPropertyNames(obj)) {
if (!fData) {
if (typeof obj[prop] === 'object' && Object.keys(obj[prop]).length > 0 && prop !== fKey) {
return getData(obj[prop], fKey, fData)
} else {
if(prop === fKey) {
fData = obj[prop];
return fData
}
}
} else {
return fData;
}
}
}
console.log(getData(jData, 'value411'));
console.log(getData(jData, 'value412'));