-1

I have following JSON format and I want to extract keys from it.

I want to get following keys

covg_AccdntDeath_Slf ,
covg_DILong_Slf ,
covg_LITwentyYr_Slf ,
covg_LITenYr_Slf ,
covg_GrpOffOvr_Slf ,
covg_LIAnnual_Slf ,
txtCampaignCode ,
associationAccronym

And this is my JSON data:

{  
  "covg_AccdntDeath_Slf":{  
    "txtGNumber":"G-29003-0",
    "txtPlanName":"Accidental Death and Dismemberment Insurance",
    "hidProdCode":"999",
    "rdTypeOfCovg":"New",
    "slidBenefitAmt":"50000",
    "productCategory":"AD"
  },
  "covg_DILong_Slf":{  
    "txtGNumber":"G-29002-0",
    "txtPlanName":"Long Term Disability",
    "hidProdCode":"601-a",
    "rdTypeOfCovg":"New",
    "txtMaxBenefitAmt":"$15,000",
    "selWaitingPeriod":"30 Days",
    "slidMonBenefitAmt":"1000",
    "productCategory":"DI"
  },
  "covg_LITwentyYr_Slf":{  
    "txtGNumber":"G-29005-0",
    "txtPlanName":"20-Year Level Term Life Insurance",
    "hidProdCode":"121",
    "rdTypeOfCovg":"New",
    "slidBenefitAmt":"100000",
    "productCategory":"LI"
  },
  "covg_LITenYr_Slf":{  
    "txtGNumber":"G-29004-0",
    "txtPlanName":"10-Year Level Term Life Insurance",
    "hidProdCode":"102",
    "rdTypeOfCovg":"New",
    "slidBenefitAmt":"100000",
    "productCategory":"LI"
  },
  "covg_GrpOffOvr_Slf":{  
    "txtGNumber":"G-29002-1",
    "txtPlanName":"Office Overhead Expense Disability Insurance",
    "hidProdCode":"603",
    "rdTypeOfCovg":"New",
    "slidBenefitAmt":"1000",
    "txtMaxBenefitAmt":"$20,000",
    "selWaitingPeriod":"30 Days",
    "selBenefitDuration":"24 months",
    "productCategory":"OO"
  },
  "covg_LIAnnual_Slf":{  
    "txtGNumber":"G-29000-0",
    "txtPlanName":"Traditional Term Life Insurance",
    "hidProdCode":"99999",
    "rdTypeOfCovg":"New",
    "slidBenefitAmt":"100000",
    "productCategory":"LI"
  },
  "txtCampaignCode":"",
  "associationAccronym":"ACS"
}

I have tried following code.

 <html>
    <head>
        <script>
            var input = above JSON String.
        var keys = [];
        console.log('Length : '+input.length);
        for(var i = 0;i<input.length;i++)
        {
            Object.keys(input[i]).forEach(function(key){
                if(keys.indexOf(key) == -1)
                {
                    keys.push(key);
                }
            });
        }
        console.log('KKKK: '+keys);
        </script>
    </head>
</html>
Jamie Eltringham
  • 810
  • 3
  • 16
  • 25
aarzoo
  • 19
  • 1
  • 5
  • 1
    Did you try objectName[keyName]???? – Goblinlord Feb 15 '16 at 15:19
  • `JSON.parse(json)` then `Object.keys(obj)` will give you an array of keys. Just remove the ones you don't want. – Andy Feb 15 '16 at 15:19
  • [Create an object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) from the JSON, then use a suitable [property accessor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors). For more complex object constructions, please see [Access nested objects, arrays or json](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json). – Teemu Feb 15 '16 at 15:21

1 Answers1

0

You don't need all that code.

1) Parse your JSON to an object: var obj = JSON.parse(json);.

2) Object don't have a length so that makes the loop redundant. Object.keys(obj) does have a length tho.

3) You're iterating over the object keys and placing them in a new array. Object.keys(obj) returns an array anyway and because keys can't be duplicates in an object the condition that checks whether the current key is in keys is also redundant.

All you need is Object.keys(obj) and then you can use forEach to iterate over the array.

Something like:

var keys = Object.keys(obj);
keys.forEach(function (el) { console.log(el); });

Or even just:

Object.keys(obj).forEach(function (el) { console.log(el); });
Andy
  • 61,948
  • 13
  • 68
  • 95