How to get json object and nested object Property names and values in AngularJs?

Viewed 1658

I have a json object like this

{
  "from": {
    "required": false,
    "type": {
      "name": {
        "required": false,
        "type": "String"
      },
      "email": {
        "required": true,
        "type": "String"
      }
    }
  },
  "to": {
    "required": true,
    "type": [
      {
        "name": {
          "required": false,
          "type": "String"
        },
        "email": {
          "required": true,
          "type": "String"
        }
      }
    ]
  },
  "subject": {
    "required": true,
    "type": "String"
  },
  "text": {
    "required": true,
    "type": "String"
  },
  "html": {
    "required": true,
    "type": "String"
  }
}

The object contains nested objects, having properties getting set from various services. So, property names are dynamically changed. I want to get property names and respective values. I'd already tried out the following code.

 for (var prop in data) {
                $scope.fieldsInfo.push({ "label": prop, "required":data.hasOwnProperty(prop) });
            }

In the above code iam getting property name (from) and required value also , now i want to get nested object property names ( name,email) and also (required,type) values inside the name object. In the above object from contains type is a object so isArray:false and next object to contains type is a array so isArray:true remaining objects type is a string so isArray:false. Output like this

$scope.fieldsInfo=[];

  $scope.fieldsInfo=[
                      { 
                       "label"    :"from",
                       "required" :false,
                       "isArray"  :false,
                       "type"     : [
                                      {
                                        "label"   :"name",
                                        "type"    : "String",
                                        "required": false
                                      }
                                      {
                                        "label"   : "email",
                                        "type"    : "String",
                                        "required": true 
                                      }                     
                                    ]
                      },
                      { 
                       "label"    :"to",
                       "required" :true,
                       "isArray"  :true,
                       "type"     : [
                                      {
                                        "label"   : "name",
                                        "type"    : "String",
                                        "required": false
                                      }
                                      {
                                        "label"   : "email",
                                        "type"    : "String",
                                        "required": true 
                                      }                       
                                    ]
                      },
                      { 
                       "label"    :"subject",
                       "required" :true,
                       "isArray"  :false,
                       "type"     :"String" 
                      },
                      { 
                       "label"    :"text",
                       "required" :true,
                       "isArray"  :false,
                       "type"     :"String" 
                      },
                      { 
                       "label"    :"html",
                       "required" :true,
                       "isArray"  :false,
                       "type"     :"String" 
                      }
                    ]

2 Answers
Related