USING IBP BPM 8.6:
I have a Json object as follows:
tw.local.stringifiedJSON = "{"name":"ahmed","age":"20","job":{"salary":"1000","position":"developer"}}";
I parsed into a javascript object:
var parsedJSONTW= JSON.parse(tw.local.stringifiedJSON);
I want to check if each key is complex (nested or has other keys and values in it like "job") or flat (has a value only like "name")
var finObj={};
var i;
for ( i in parsedJSONTW) {
if (finObj[i] === undefined) { finObj[i] = {}; }
tw.local.propertiesOfObject=Object.getOwnPropertyNames(parsedJSONTW[i]);
if(tw.local.propertiesOfObject==null || tw.local.propertiesOfObject.listLength==0)
{
finObj[i]= parsedJSONTW[i]; //expected to have name and age fields only
}
Using object.getOwnProperty() doesn't work with a flat object and gives the error of "expected an object but found a string"
You can use "typeof" for this:
let a = {"name":"ahmed","age":"20","job":{"salary":"1000","position":"developer"}}
for(let k in a){
console.log(typeof a[k])
if(typeof a[k] === 'object'){
console.log("nested")
}
else{
console.log("flat")
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.