ANKITESH JHA ANKITESH JHA - 3 months ago 8
JSON Question

I have a JSON object that is dymanic in length, need to process it

I have a JSON response that comes like this ::

{
"abc": [{
"statsFetchDate": 1463961600000,
"facebookLikes": 0,
"facebookComments": 0,
"facebookShares": 0,
"twitterShares": 0,
"linkedInShares": null,
"instagramLikes": null,
"instagramComments": null,
"engagement": null,
"velocity": 50710.29999999996
}, {
"statsFetchDate": 1464048000000,
"facebookLikes": 0,
"facebookComments": 0,
"facebookShares": 0,
"twitterShares": 0,
"linkedInShares": null,
"instagramLikes": null,
"instagramComments": null,
"engagement": null,
"velocity": 473968.7000000002
}],
"def": [{
"statsFetchDate": 1463961600000,
"facebookLikes": 0,
"facebookComments": 0,
"facebookShares": 0,
"twitterShares": 0,
"linkedInShares": null,
"instagramLikes": null,
"instagramComments": null,
"engagement": null,
"velocity": 50710.29999999996
}, {
"statsFetchDate": 1464048000000,
"facebookLikes": 0,
"facebookComments": 0,
"facebookShares": 0,
"twitterShares": 0,
"linkedInShares": null,
"instagramLikes": null,
"instagramComments": null,
"engagement": null,
"velocity": 473968.7000000002
}]
}


i need to plot graphs for each of the object that comes, ie abc and def in this case, but the json objects are dynamic in nature and i can get multiple objects, how can i parese through each of them and process them for example here i need to access abc.stats fetch date and velocity and also def.stats fetch date and velocity, it will be a multiple line chart, plotting is not a tension but how can i parse through mutiple JSON objects?

Answer

Check the code below which will console.log the each statsFetchDate in your nested JSON. Check Fiddle

var obj = {
  "abc": [{
    "statsFetchDate": 1463961600000,
    "facebookLikes": 0,
    "facebookComments": 0,
    "facebookShares": 0,
    "twitterShares": 0,
    "linkedInShares": null,
    "instagramLikes": null,
    "instagramComments": null,
    "engagement": null,
    "velocity": 50710.29999999996
  }, {
    "statsFetchDate": 1464048000000,
    "facebookLikes": 0,
    "facebookComments": 0,
    "facebookShares": 0,
    "twitterShares": 0,
    "linkedInShares": null,
    "instagramLikes": null,
    "instagramComments": null,
    "engagement": null,
    "velocity": 473968.7000000002
  }],
  "def": [{
    "statsFetchDate": 1463961600000,
    "facebookLikes": 0,
    "facebookComments": 0,
    "facebookShares": 0,
    "twitterShares": 0,
    "linkedInShares": null,
    "instagramLikes": null,
    "instagramComments": null,
    "engagement": null,
    "velocity": 50710.29999999996
  }, {
    "statsFetchDate": 1464048000000,
    "facebookLikes": 0,
    "facebookComments": 0,
    "facebookShares": 0,
    "twitterShares": 0,
    "linkedInShares": null,
    "instagramLikes": null,
    "instagramComments": null,
    "engagement": null,
    "velocity": 473968.7000000002
  }]
}
var objKeys = Object.keys(obj)
for (var i = 0, len = objKeys.length; i < len; i++) {
  for (var y = 0, len = obj[objKeys[i]].length; y < len; y++) {
    console.log(obj[objKeys[i]][y].statsFetchDate)
  }
}