3

I need to search through JSON data and find every object that contains the "kind" "playlist" and then go through the tracks and pull information which will be put into a hmtl list. The problem is that in some instances (depending on the type of URL) instead of a multidimensional array containing all the objects, the json information is just a singular object.

Below are the two url types.

This is a playlist which contains just one level of information. http://api.soundcloud.com/playlists/1980151.json?client_id=bcc776e7aa65dbc29c40ff21a1a94ecd

This is an array which contains just multiple playlists as objects. http://api.soundcloud.com/users/dubstep/playlists.json?client_id=bcc776e7aa65dbc29c40ff21a1a94ecd

The current code I have is this:

$.getJSON('http://api.soundcloud.com/users/dubstep/playlists.json?client_id=bcc776e7aa65dbc29c40ff21a1a94ecd', { get_param: 'value' }, function(url_data) {
$.each(url_data, function (i, elem) {
    if (elem.kind === 'playlist') {
        $.each(elem.tracks, function (i, elem) {
            console.log(elem.title);
        });
    }
});

});

It only works when dealing with "user" urls where there are multiple playlists.

To sum up what my issue is, I need a way to search through all the levels of an array to find levels with the kind === playlist.

3 Answers 3

12

This should do what you're looking for.

$.each(json, function (i, elem) {
    if (elem.kind === 'playlist') {
        $.each(elem.tracks, function (i, elem) {
            console.log(elem.title);
        });
    }
});

UPDATE:

This will work with either URL. Also, here's a fiddle with more a more advanced client-side output: http://jsfiddle.net/jTLvE/

var parse = function (json) {
    $.each(json, function (i, elem) {
        if (elem.kind === 'playlist') {
            $.each(elem.tracks, function (i, elem) {
                console.log(elem.title);
            });
        }
    });
},
parseReturnedData = function (json) {
    var isObject = (typeof json === 'object' && json instanceof Object),
        isArray = (typeof json === 'object' && json instanceof Array),
        newArray = [];
    if (isArray) {
        parse(json);
    } else if (isObject) {
        newArray.push(json);
        parse(newArray);
    }
};
Sign up to request clarification or add additional context in comments.

2 Comments

This works perfectly when I have multiple playlists, perfectly! However if I use api.soundcloud.com/playlists/… it won't work because "TypeError: 'null' is not an object (evaluating 'elem.kind')"
Wow, this went above and beyond the call! This is perfect. Cheers!
0

Have you tried json path? I used it and it's small and fast, especially for searching json trees (for searching and writing I'd use flock though).

2 Comments

I'd prefer to not have this code rely on any plugins or requirements aside from jquery as I'd like to use it as a standalone plugin.
I do understand your concerns; should you need to do json lookups I think it's worth checking these libraries out, but of course for a single case it may be an overkill.
-1

try this code:

var objJson = jQuery.parseJSON(json);
jQuery.each(objJson , function(i, val) {
      alert(val);
    });

Comments

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.