Home:ALL Converter>Check if a json key is a complex object javascript

Check if a json key is a complex object javascript

Ask Time:2018-11-11T18:15:55         Author:User MA

Json Formatter

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"

Author:User MA,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53247727/check-if-a-json-key-is-a-complex-object-javascript
yy