Q:

trying to access keys and values from a nested json - javascript

I have a nested json which looks like this:
{'services': 
    {
        'serviceA': 'OptionA',
        'serviceB': 'OptionB',
        'serviceC': null
    },
'AttributeB': 'ValueB',
'AttributeC': 'ValueC'
}

I would like to render nested json (services) in a tabular format like this:
services     option
serviceA     OptionA
serviceB     OptionB
serviceC
I was trying something like this: 
var myJSONObject =  {'services': 
        {
            'serviceA': 'OptionA',
            'serviceB': 'OptionB',
            'serviceC': null
        },
    'AttributeB': 'ValueB',
    'AttributeC': 'ValueC'
    };

for(var i = 0; i < myJSONObject.services.length; i++)
{
    var product = myJSONObject.services[i];
    alert(product.key);
    alert(product.value);
}

This doesn't seem to help.  I am guessing I am retrieving the object in an incorrect manner.  Can somebody help? 

A:

services is an object and you need to iterate over its properties.  For that you can try following
var myJSONObject =  {'services': 
        {
            'serviceA': 'OptionA',
            'serviceB': 'OptionB',
            'serviceC': null
        },
    'AttributeB': 'ValueB',
    'AttributeC': 'ValueC'
    };

for(var key in myJSONObject.services)
{
    alert(key);
    alert(myJSONObject.services[key]);
}

