title: How to choose a JSON data node dynamically with jQuery
body: <p>I have an ajax function as follows:-</p>
  
  <pre><code>$.ajax({
      type: "GET",
      dataType: "json",
      async: false,
      url: "/wp-content/comment_data.php",
      data: 'songid=' + $array,
      success: function(data){
          oTable.find('td').each(function(){
              $id = $(this).data('songid');
              songid = 'songid-' + $id;
              $commentbar = $(this).find('#comment-bar' + $id);
              array = data.songid;
  
              $.tmpl( "commentBarTemplate", array ).appendTo($commentbar);
          });     
      }
  }); 
  </code></pre>
  
  <p>What this does is collects a list of id's on the page (the songid of each 'td'), sends the array to my server side script where everything is processed and a JSON response containing comments/replies is returned for each songid.  Ass you can see each array of comments/replies is stored in the array under 'songid-24', 'songid-23' etc...</p>
  
  <p>My PHP script returns the following JSON:</p>
  
  <pre><code>{ "songid-22" : [  ],
    "songid-23" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "test",
          "display_name" : "admin",
          "id" : "5",
          "playtime" : "56",
          "posttime" : "2011-09-28 15:32:41",
          "songid" : "23",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "test comment",
          "display_name" : "admin",
          "id" : "6",
          "playtime" : "87",
          "posttime" : "2011-09-28 16:38:37",
          "songid" : "23",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        }
      ],
    "songid-24" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "comment at 0:48",
          "display_name" : "admin",
          "id" : "2",
          "playtime" : "48",
          "posttime" : "2011-09-28 14:38:41",
          "replies" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
                "body" : "haha reply",
                "cid" : "2",
                "display_name" : "admin",
                "id" : "1",
                "posttime" : "2011-09-28 15:14:56",
                "uid" : "1",
                "user_login" : "admin",
                "user_url" : "http://www.songbanc.com/members/admin/"
              },
              { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
                "body" : "next replty",
                "cid" : "2",
                "display_name" : "admin",
                "id" : "2",
                "posttime" : "2011-09-28 15:15:07",
                "uid" : "1",
                "user_login" : "admin",
                "user_url" : "http://www.songbanc.com/members/admin/"
              },
              { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
                "body" : "first reply",
                "cid" : "2",
                "display_name" : "admin",
                "id" : "3",
                "posttime" : "2011-09-28 15:15:15",
                "uid" : "1",
                "user_login" : "admin",
                "user_url" : "http://www.songbanc.com/members/admin/"
              }
            ],
          "songid" : "24",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "asdasd",
          "display_name" : "admin",
          "id" : "3",
          "playtime" : "2",
          "posttime" : "2011-09-28 14:43:11",
          "replies" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
                "body" : "test reply",
                "cid" : "3",
                "display_name" : "admin",
                "id" : "5",
                "posttime" : "2011-10-06 14:35:23",
                "uid" : "1",
                "user_login" : "admin",
                "user_url" : "http://www.songbanc.com/members/admin/"
              },
              { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
                "body" : "test reply",
                "cid" : "3",
                "display_name" : "admin",
                "id" : "6",
                "posttime" : "2011-10-07 14:48:21",
                "uid" : "1",
                "user_login" : "admin",
                "user_url" : "http://www.songbanc.com/members/admin/"
              }
            ],
          "songid" : "24",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "test",
          "display_name" : "admin",
          "id" : "10",
          "playtime" : "97",
          "posttime" : "2011-10-06 14:15:12",
          "songid" : "24",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        }
      ],
    "songid-25" : [  ],
    "songid-26" : [  ],
    "songid-27" : [  ],
    "songid-28" : [  ],
    "songid-29" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "hello hello!",
          "display_name" : "admin",
          "id" : "24",
          "playtime" : "55",
          "posttime" : "2011-10-08 10:54:37",
          "replies" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
                "body" : "further testing!",
                "cid" : "24",
                "display_name" : "admin",
                "id" : "12",
                "posttime" : "2011-10-08 10:54:48",
                "uid" : "1",
                "user_login" : "admin",
                "user_url" : "http://www.songbanc.com/members/admin/"
              },
              { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
                "body" : "test reply",
                "cid" : "24",
                "display_name" : "test user 2",
                "id" : "13",
                "posttime" : "2011-10-08 11:12:42",
                "uid" : "3",
                "user_login" : "testing",
                "user_url" : "http://www.songbanc.com/members/testing/"
              }
            ],
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "Awesome!",
          "display_name" : "admin",
          "id" : "25",
          "playtime" : "135",
          "posttime" : "2011-10-08 10:54:58",
          "replies" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
                "body" : "geggegge",
                "cid" : "25",
                "display_name" : "test user 2",
                "id" : "16",
                "posttime" : "2011-10-24 20:04:33",
                "uid" : "3",
                "user_login" : "testing",
                "user_url" : "http://www.songbanc.com/members/testing/"
              } ],
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
          "body" : "testing...",
          "display_name" : "test user 2",
          "id" : "27",
          "playtime" : "35",
          "posttime" : "2011-10-08 11:13:01",
          "replies" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
                "body" : "grgrgehhehehe",
                "cid" : "27",
                "display_name" : "admin",
                "id" : "14",
                "posttime" : "2011-10-11 15:22:02",
                "uid" : "1",
                "user_login" : "admin",
                "user_url" : "http://www.songbanc.com/members/admin/"
              },
              { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
                "body" : "testing",
                "cid" : "27",
                "display_name" : "admin",
                "id" : "15",
                "posttime" : "2011-10-11 18:23:01",
                "uid" : "1",
                "user_login" : "admin",
                "user_url" : "http://www.songbanc.com/members/admin/"
              },
              { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
                "body" : "another",
                "cid" : "27",
                "display_name" : "admin",
                "id" : "20",
                "posttime" : "2011-11-25 14:52:02",
                "uid" : "1",
                "user_login" : "admin",
                "user_url" : "http://www.songbanc.com/members/admin/"
              }
            ],
          "songid" : "29",
          "uid" : "3",
          "user_login" : "testing",
          "user_url" : "http://www.songbanc.com/members/testing/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "Just testing out the commenting functionality and seeing how good it is now that i\\'ve modified the styling etc...",
          "display_name" : "admin",
          "id" : "29",
          "playtime" : "80",
          "posttime" : "2011-10-11 00:46:58",
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "hfhhghfhhhhshfdhsh",
          "display_name" : "admin",
          "id" : "30",
          "playtime" : "110",
          "posttime" : "2011-10-11 15:21:42",
          "replies" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
                "body" : "thrthhrht",
                "cid" : "30",
                "display_name" : "admin",
                "id" : "19",
                "posttime" : "2011-11-10 14:19:03",
                "uid" : "1",
                "user_login" : "admin",
                "user_url" : "http://www.songbanc.com/members/admin/"
              } ],
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "testing......",
          "display_name" : "admin",
          "id" : "31",
          "playtime" : "154",
          "posttime" : "2011-10-13 02:10:42",
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "hello",
          "display_name" : "admin",
          "id" : "32",
          "playtime" : "94",
          "posttime" : "2011-10-13 02:12:27",
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "blah",
          "display_name" : "admin",
          "id" : "33",
          "playtime" : "121",
          "posttime" : "2011-10-13 02:16:25",
          "replies" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
                "body" : "eeffefef",
                "cid" : "33",
                "display_name" : "test user 2",
                "id" : "17",
                "posttime" : "2011-10-24 20:04:48",
                "uid" : "3",
                "user_login" : "testing",
                "user_url" : "http://www.songbanc.com/members/testing/"
              } ],
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "comment",
          "display_name" : "admin",
          "id" : "34",
          "playtime" : "70",
          "posttime" : "2011-10-13 02:17:54",
          "replies" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
                "body" : "feffefee",
                "cid" : "34",
                "display_name" : "test user 2",
                "id" : "18",
                "posttime" : "2011-10-24 20:04:56",
                "uid" : "3",
                "user_login" : "testing",
                "user_url" : "http://www.songbanc.com/members/testing/"
              } ],
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "commenting",
          "display_name" : "admin",
          "id" : "35",
          "playtime" : "20",
          "posttime" : "2011-10-13 02:20:31",
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "ffgwgwgwgwgw",
          "display_name" : "admin",
          "id" : "36",
          "playtime" : "102",
          "posttime" : "2011-10-13 02:21:30",
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
          "body" : "this is a test comment to see if avatars work",
          "display_name" : "test user 2",
          "id" : "64",
          "playtime" : "62",
          "posttime" : "2011-10-13 11:46:45",
          "songid" : "29",
          "uid" : "3",
          "user_login" : "testing",
          "user_url" : "http://www.songbanc.com/members/testing/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
          "body" : "comment",
          "display_name" : "test user 2",
          "id" : "65",
          "playtime" : "170",
          "posttime" : "2011-10-13 12:21:51",
          "songid" : "29",
          "uid" : "3",
          "user_login" : "testing",
          "user_url" : "http://www.songbanc.com/members/testing/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
          "body" : "rtrgrrg",
          "display_name" : "test user 2",
          "id" : "66",
          "playtime" : "44",
          "posttime" : "2011-10-13 13:01:37",
          "songid" : "29",
          "uid" : "3",
          "user_login" : "testing",
          "user_url" : "http://www.songbanc.com/members/testing/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
          "body" : "hello world",
          "display_name" : "test user 2",
          "id" : "67",
          "playtime" : "178",
          "posttime" : "2011-10-13 13:44:17",
          "songid" : "29",
          "uid" : "3",
          "user_login" : "testing",
          "user_url" : "http://www.songbanc.com/members/testing/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "comment time!!! YAY!!",
          "display_name" : "admin",
          "id" : "68",
          "playtime" : "48",
          "posttime" : "2011-10-13 15:07:56",
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "hello world...",
          "display_name" : "admin",
          "id" : "72",
          "playtime" : "86",
          "posttime" : "2011-10-13 17:18:58",
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "ygygyigyiugyugyuig",
          "display_name" : "admin",
          "id" : "73",
          "playtime" : "28",
          "posttime" : "2011-10-14 11:07:27",
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "test\n",
          "display_name" : "admin",
          "id" : "74",
          "playtime" : "146",
          "posttime" : "2011-10-24 19:40:36",
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/3/47459545ffdbdfffa7620ce562e44c54-bpthumb.jpg",
          "body" : "htththth",
          "display_name" : "test user 2",
          "id" : "76",
          "playtime" : "197",
          "posttime" : "2011-10-24 20:05:47",
          "songid" : "29",
          "uid" : "3",
          "user_login" : "testing",
          "user_url" : "http://www.songbanc.com/members/testing/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "fdgrttgrhrgrgrt",
          "display_name" : "admin",
          "id" : "77",
          "playtime" : "131",
          "posttime" : "2011-11-10 14:18:12",
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "hello",
          "display_name" : "admin",
          "id" : "78",
          "playtime" : "190",
          "posttime" : "2011-11-10 14:18:52",
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "testing",
          "display_name" : "admin",
          "id" : "79",
          "playtime" : "117",
          "posttime" : "2011-11-17 08:50:38",
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "hfdhfdhfdhdhfhhfdh",
          "display_name" : "admin",
          "id" : "80",
          "playtime" : "74",
          "posttime" : "2011-11-18 17:29:39",
          "songid" : "29",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        }
      ],
    "songid-30" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "More tests....",
          "display_name" : "admin",
          "id" : "26",
          "playtime" : "126",
          "posttime" : "2011-10-08 11:11:55",
          "songid" : "30",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "I\\'m just testing the automatic slash  stripper \\\"hello\\\", \\'yay\\' !!! it works!!",
          "display_name" : "admin",
          "id" : "70",
          "playtime" : "103",
          "posttime" : "2011-10-13 15:51:01",
          "songid" : "30",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        },
        { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
          "body" : "Another genius comment....",
          "display_name" : "admin",
          "id" : "71",
          "playtime" : "170",
          "posttime" : "2011-10-13 15:55:38",
          "songid" : "30",
          "uid" : "1",
          "user_login" : "admin",
          "user_url" : "http://www.songbanc.com/members/admin/"
        }
      ]
  }
  </code></pre>
  
  <p>What I am trying to do once I have recieved the JSON response, is then iterate back through each 'td' and append a jQuery template to each one.</p>
  
  <p>The problem is that I need to dynamically select the data key i.e data.songid-24, data.songid-23 etc.</p>
  
  <p>Within the each loop I am able to collect the id itself as shown in the code as $id as I have it stored on each 'td' by using the jquery.data method.</p>
  
  <p>Any ideas?</p>