If I having a nested JSON I wanted all the keys traversal path,
like for
var a={
"a":"b",
"c":{
"d":"e"
}
}
const keyify = (obj, prefix = '') =>
Object.keys(obj).reduce((res, el) => {
if( Array.isArray(obj[el]) ) {
return res;
} else if( typeof obj[el] === 'object' && obj[el] !== null ) {
return [...res, ...keyify(obj[el], prefix + el + '.')];
} else {
return [...res, prefix + el];
}
}, []);
const output = keyify(a);
console.log(output);
Output will be:
[ "a", "c.d" ]
In the above code it'll return all keys if it is not having object or array I means if it having string/integer/boolean. But for the below payload it is not giving any output.
var a=
{
"paramList": [
{
"parameterName": "temperature",
"parameterpresentValue": "25.32",
"parameterunits": "°C"
},
{
"parameterName": "Pressure",
"parameterpresentValue": "1004.08",
"parameterunits": "mBar"
}]
}
Expected output:
["paramList.parameterName","paramList.parameterpresentValue","paramList.parameterunits"]