1

I get this JSON array via AJAX:

{
    "success": 1,
    "message": {
        "ar": {
            "total": 747.4851,
            "list": [{
                "total_statements": 1,
                "total_due": 747.4851,
                "name": "Paul McBilling",
                "connection_id": 154
            }]
        },
        "ap": {
            "total": 0,
            "list": []
        },
        "graphs": {
            "graph": {
                "2016-01": 13,
                "2016-02": 0,
                "2016-03": 0,
                "2016-04": 747.4851,
                "2016-05": 0,
                "2016-06": 0,
                "2016-07": 0,
                "2016-08": 0,
                "2016-09": 0,
                "2016-10": 0,
                "2016-11": 0,
                "2016-12": 0
            }
        },
        "cached": 1
    }
}

And the result I would like to get back would look something like this (in the console):

2016-01: 13
2016-02: 0
... 

And I'm trying to log each key and value pair of the inner "graph" array.

I've tried this:

$(data.message.graphs.graph).each(function(key, value){
    console.log(key +' and '+ value);
});

But what this returns is only once:

0 and [object Object]
1
  • I think you should use each function one by one for the nested elements to get the 'graph' array in key value pair. Commented Apr 23, 2016 at 18:47

2 Answers 2

3

You need to use jQuery.each() for iterating object

var data = {
  "success": 1,
  "message": {
    "ar": {
      "total": 747.4851,
      "list": [{
        "total_statements": 1,
        "total_due": 747.4851,
        "name": "Paul McBilling",
        "connection_id": 154
      }]
    },
    "ap": {
      "total": 0,
      "list": []
    },
    "graphs": {
      "graph": {
        "2016-01": 13,
        "2016-02": 0,
        "2016-03": 0,
        "2016-04": 747.4851,
        "2016-05": 0,
        "2016-06": 0,
        "2016-07": 0,
        "2016-08": 0,
        "2016-09": 0,
        "2016-10": 0,
        "2016-11": 0,
        "2016-12": 0
      }
    },
    "cached": 1
  }
}

$.each(data.message.graphs.graph,function(key, value) {
  console.log(key + ' and ' + value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


Or in pure JavaScript using Object.keys() to get keys array and forEach() for iterating it.

var data = {
  "success": 1,
  "message": {
    "ar": {
      "total": 747.4851,
      "list": [{
        "total_statements": 1,
        "total_due": 747.4851,
        "name": "Paul McBilling",
        "connection_id": 154
      }]
    },
    "ap": {
      "total": 0,
      "list": []
    },
    "graphs": {
      "graph": {
        "2016-01": 13,
        "2016-02": 0,
        "2016-03": 0,
        "2016-04": 747.4851,
        "2016-05": 0,
        "2016-06": 0,
        "2016-07": 0,
        "2016-08": 0,
        "2016-09": 0,
        "2016-10": 0,
        "2016-11": 0,
        "2016-12": 0
      }
    },
    "cached": 1
  }
}

Object.keys(data.message.graphs.graph).forEach(function(k) {
  console.log(k + ' and ' + data.message.graphs.graph[k]);
});


Or even you can use for...in loop

var data = {
  "success": 1,
  "message": {
    "ar": {
      "total": 747.4851,
      "list": [{
        "total_statements": 1,
        "total_due": 747.4851,
        "name": "Paul McBilling",
        "connection_id": 154
      }]
    },
    "ap": {
      "total": 0,
      "list": []
    },
    "graphs": {
      "graph": {
        "2016-01": 13,
        "2016-02": 0,
        "2016-03": 0,
        "2016-04": 747.4851,
        "2016-05": 0,
        "2016-06": 0,
        "2016-07": 0,
        "2016-08": 0,
        "2016-09": 0,
        "2016-10": 0,
        "2016-11": 0,
        "2016-12": 0
      }
    },
    "cached": 1
  }
}
for (var k in data.message.graphs.graph) {
  if (data.message.graphs.graph.hasOwnProperty(k)) {
    console.log(k + ' and ' + data.message.graphs.graph[k]);
  }
}

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Pranav! That was fast :)
@Frank : glad to help
2

You can use javascript for like following.

var data = {
  "success": 1,
  "message": {
    "ar": {
      "total": 747.4851,
      "list": [{
        "total_statements": 1,
        "total_due": 747.4851,
        "name": "Paul McBilling",
        "connection_id": 154
      }]
    },
    "ap": {
      "total": 0,
      "list": []
    },
    "graphs": {
      "graph": {
        "2016-01": 13,
        "2016-02": 0,
        "2016-03": 0,
        "2016-04": 747.4851,
        "2016-05": 0,
        "2016-06": 0,
        "2016-07": 0,
        "2016-08": 0,
        "2016-09": 0,
        "2016-10": 0,
        "2016-11": 0,
        "2016-12": 0
      }
    },
    "cached": 1
  }
}

var graph = data.message.graphs.graph;
for (var key in graph) {
  console.log(key + ' and ' + graph[key]);
}

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.