Question

I'm working with a response from the Webtrends API in Google apps script and I have a JSON/JS object that looks like this:

"data": [
    {
        "period": "Month",
        "start_date": "2013-12",
        "end_date": "2013-12",
        "attributes": {},
        "measures": {
            "Visits": 500
        },
        "SubRows": [
            {
                "facebook.com": {
                    "attributes": {},
                    "measures": {
                        "Visits": 100
                    },
                    "SubRows": null
                },
                "google.co.uk": {
                    "attributes": {},
                    "measures": {
                        "Visits": 100
                    },
                    "SubRows": null
                },
                "newsnow.co.uk": {
                    "attributes": {},
                    "measures": {
                        "Visits": 100
                    },
                    "SubRows": null
                },
                "No Referrer": {
                    "attributes": {},
                    "measures": {
                        "Visits": 100
                    },
                    "SubRows": null
                },
                "t.co": {
                    "attributes": {},
                    "measures": {
                        "Visits": 100
                    },
                    "SubRows": null
                }
            }
        ]
    }
]

What I need to access is the names i.e facebook.com etc... and visit numbers for each of the SubRows.

I'm able to get the visit numbers, but I can't work out how to get the names. Please note the names will change constantly as different sites will send different amounts of traffic each day.

Section of my code at the moment where I get the visit numbers:

for(i in dObj){
var data = dObj[i].SubRows;
var sd = dObj[i].start_date;
var ed = dObj[i].end_date;

if(sd == ed){
    var timep = ""+ sd;
  }
  else{
    var timep = ""+ sd + "-" + ed;
  }

var subRows = data[0];

Logger.log(subRows);

for(i in subRows){

  var row = subRows[i];
  var rmeasures = row.measures;
  var rvis = rmeasures.Visits;

  values = [timep,"",rvis]; //Blank string for where the name of the site would go
}

}

I've tried the following links, but none of them seem to have the answer:

Getting JavaScript object key list How to access object using dynamic key? How to access key itself using javascript How do I access properties of a javascript object if I don't know the names?

I'm just using vanilla google apps script as I don't have any experience with Jquery etc...

Any help would be much appreciated!

Was it helpful?

Solution 2

Within the loop, the variable i contains the current key. Replacing the empty string with i should give you what you need.

OTHER TIPS

I usually use a little helper function that looks like this:

var keyVal = function(o) {
    var key = Object.keys(o)[0];
    return {"key": key, "val":o[key]};
} ;

This will map an object with a variable key to a key/value object {key:...., val:{}}, which is usually convenient enough to work with.

describe.only ("stack overflow answer", function(){

    it ("is should create a key/value pair" , function(){

        var res = keyVal(  {
            "facebook.com": {
                "attributes": {},
                "measures": {
                    "Visits": 100
                },
                "SubRows": null
            }});
        res.key.should.equal('facebook.com');
        res.val.attributes.should.deep.equal({});
    });

You might also want to look at some of the more functional tools built into Javascript. Some more concise code might also be more explicit:

data.map(function(datum) {
    var timep = datum.start_date == datum.end_date ? datum.end_date : 
            (data.start_date + "-" + datum.end_date);  
    return datum.SubRows.map(function(subRow) {
        return Object.keys(subRow).map(function(key) {
            return [timep, key, subRow[key].measures.Visits];
        });
    });
});

would return an object something like this:

[
    [
        [
            ["2013-12", "facebook.com", 100],
            ["2013-12", "google.co.uk", 100],
            ["2013-12", "newsnow.co.uk", 100],
            ["2013-12", "No Referrer", 100],
            ["2013-12", "t.co", 100 ]
        ]
    ]
] 

This just uses map and Object.keys to simplify some of what you're doing with explicit loops.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top