0

Hi I'm trying to do some charting with Google Analytics and I can get the data into JSON but I get stuck from there, this is the json as seen in firebug.

{"total_results":1,"rows":[[7861]],"column_headers":[{"dataType":"INTEGER","columnType":"METRIC","name":"ga:visits"}]}

So I'm trying to extract the fact that gavisits has 7842 results for the last 30 days, this is my code

$.getJSON(
"https://api.oocharts.com/v1/query.jsonp?" //note the 'jsonp' return type
+ "query=" + "visits-by-date-cg"
+ "&key=" + "myapikey here"
+ "&start=" + "30d" //30 days from current date
+ "&callback=?"
, null, function(data) {
    console.log(JSON.stringify(data));
});

How do I loop through the 'data' to get 7842 so that I can store this as a variable?

Thank you

4
  • 2
    The thing you posted above doesn't look like a JSON. Do console.log(JSON.stringify(json)) Commented Jul 31, 2013 at 20:00
  • Yeah, that's not JSON. Commented Jul 31, 2013 at 20:27
  • 1
    thanks man, the JSON.stringify gave me a more legible output, I have updated the result above Commented Jul 31, 2013 at 20:47
  • McGarnagle that did the trick without me having to use JSON.stringify, nice work, thank you Commented Jul 31, 2013 at 20:57

1 Answer 1

1

You won't find 7842 anywhere in that object, but there is a property called rows containing an array with one value, which itself is an array with one value, which is 7861. That's probably what you're looking for:

data.rows[0][0]
Sign up to request clarification or add additional context in comments.

1 Comment

yes sorry 7861 is what I was looking for. Thanks for the correct answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.