0

I have the following valid JSON

[
    {
        "series": [
            {
                "name": "Comp",
                "data": [
                    753,
                    384,
                    864,
                    654
                ],
                "color": "#FFAC3F"
            },
            {
                "name": "Ind",
                "data": [
                    642,
                    456,
                    983,
                    564
                ]
            },
            {
                "name": "Store",
                "data": [
                    832,
                    243,
                    646,
                    777
                ],
                "color": "#FF0000"
            }
        ]
    }
]

I want to return all the data inside "series" but doing something like say assigning this to a variable and then using data.series is not returning anything. How would I do this using jquery or javascript? This data is actually getting returned in a jquery JSON ajax request like so....

$.getJSON(url,  function(data) {
3
  • 1
    If you're calling $.getJSON(), you don't have to parse it at all. The parsed object is passed to your callback function ready-to-use. Commented Dec 3, 2013 at 1:07
  • i thought so too but when i do alert(data.series) it is not alerting anything except [Object] Commented Dec 3, 2013 at 1:08
  • Right - see the answers to your question :) Commented Dec 3, 2013 at 1:09

2 Answers 2

5

Your JSON is wrapped in an array. You want

data[0].series

For a better visual, review this

var data = [{foo: "bar"}, {hello: "world"}];

To get foo, you would use

data[0].foo; // "bar"

To get hello, you would use

data[1].hello; // "world"

Considering you have

var data = [{series: [...]}];

You need to use

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

1 Comment

Got it thanks, painful though, to do something like data[0].series[1].name what if I wanted all the series data like [ { "name": "Comp", "data": [ 753, 384, 864, 654 ], "color": "#FFAC3F" }, is there some tostring function or something?
2
$.getJSON(url, function(data) {
    console.log(data[0].series)
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.