0

I am creating an app using sound cloud javascript api. What i am doing getting the json from

http://api.soundcloud.com/resolve.json?url=http://soundcloud.com/matas/hobnotropic&client_id=YOUR_CLIENT_ID

This works pretty well and also returns the desired things but how ever i want to add a new variable to the returned json output.

Currently the output looks like the following

{"kind":"track","id":49931,"created_at":"2008/10/27 09:56:47 +0000","user_id":1433,"duration":489138,"commentable":true,"state":"finished","original_content_size":130032044,"sharing":"public","tag_list":"dub minimalist hypnotic flanger","permalink":"hobnotropic","streamable":true,"embeddable_by":"all","downloadable":false,"purchase_url":"https://soundcloud.com/matas","label_id":null,"purchase_title":"Get more tracks!","genre":"dub","title":"Hobnotropic","description":"Kinda of an experiment in search for my own sound. I've produced this track from 2 loops I've made using Hobnox Audiotool ( http://www.hobnox.com/audiotool.1046.en.html ). Imported into Ableton LIve! and tweaked some FX afterwards.","label_name":"","release":"","track_type":"original","key_signature":"","isrc":"","video_url":null,"bpm":84.0,"release_year":null,"release_month":null,"release_day":null,"original_format":"wav","license":"cc-by-nc","uri":"http://api.soundcloud.com/tracks/49931","user":{"id":1433,"kind":"user","permalink":"matas","username":"matas","uri":"http://api.soundcloud.com/users/1433","permalink_url":"http://soundcloud.com/matas","avatar_url":"http://i1.sndcdn.com/avatars-000001548772-zay6dy-large.jpg?16b9957"},"permalink_url":"http://soundcloud.com/matas/hobnotropic","artwork_url":"http://i1.sndcdn.com/artworks-000000103093-941e7e-large.jpg?16b9957","waveform_url":"http://w1.sndcdn.com/IqSLUxN7arjs_m.png","stream_url":"http://api.soundcloud.com/tracks/49931/stream","playback_count":74836,"download_count":280,"favoritings_count":25,"comment_count":23,"attachments_uri":"http://api.soundcloud.com/tracks/49931/attachments"}

What i want is just to add a variable like "custom_url":"http://example.com/example123"

So can anyone guide me that is there any possible way to do this. I am wondering if i could just pass the variable to soundcloud and soundcloud would automatically append the variable to the response.

Nirmal Ram
  • 1,722
  • 4
  • 25
  • 45
  • 1
    So you have a javascript object, lets call it `result`, and you want to add a property to it? Have you tried `result.custom_url = 'http://example.com/example123'`? – Jason P Aug 23 '13 at 12:29
  • Yes i have tried that and it only works but if its a playlist then there is a problem. As there are multiple results inside a single result and i need to do it for every result. Do you understand what i mean ? – Nirmal Ram Aug 23 '13 at 12:33
  • take a look at [`jQuery.each()`](http://api.jquery.com/jQuery.each/) – NDM Aug 23 '13 at 12:35

3 Answers3

0

First, store the JSON response in a variable:

var obj = response;

So, you now have a variable which holds a javascript object. To add properties to your object you simple use:

obj.custom_url = "Your_url";

or

obj["custom_url"] = "your_url";
Divij Bindlish
  • 325
  • 1
  • 8
0

I don't know the full object structure, but lets say the object is called result and there is a property called albums, which is an array of objects. In that case, you would do this:

$.each(result.albums, function() {
    this.custom_url = 'whatever';
});

Or with vanilla js:

for (var i = 0, i < result.albums.length; i++) {
    result.albums[i].custom_url = 'whatever';
}
Jason P
  • 26,984
  • 3
  • 31
  • 45
-1

u don't need or want jQuery for this, just JavaScript.

take a example

var data = {items: [
    {id: "1", name: "Snatch", type: "crime"},
    {id: "2", name: "Witches of Eastwick", type: "comedy"},
    {id: "3", name: "X-Men", type: "action"},
    {id: "4", name: "Ordinary People", type: "drama"},
    {id: "5", name: "Billy Elliot", type: "drama"},
    {id: "6", name: "Toy Story", type: "children"}
]};

Adding an item:

data.items.push(
    {id: "7", name: "Douglas Adams", type: "comedy"}
);

That adds to the end. See below for adding in the middle.

Removing an item:

There are several ways. The splice method is the most versatile:

data.items.splice(1, 3); // Removes three items starting with the 2nd,
                         // ("Witches of Eastwick", "X-Men", "Ordinary People")

splice modifies the original array, and returns an array of the items you removed.

Adding in the middle:

splice actually does both adding and removing. The signature of the splice method is:

removed_items = arrayObject.splice(index, num_to_remove[, add1[, add2[, ...]]]);

index - the index at which to start making changes
num_to_remove - starting with that index, remove this many entries
addN - ...and then insert these elements

So I can add an item in the 3rd position like this:

data.items.splice(2, 0,
    {id: "7", name: "Douglas Adams", type: "comedy"}
);

What that says is: Starting at index 2, remove zero items, and then insert this following item. The result looks like this:

var data = {items: [
    {id: "1", name: "Snatch", type: "crime"},
    {id: "2", name: "Witches of Eastwick", type: "comedy"},
    {id: "7", name: "Douglas Adams", type: "comedy"},     // <== The new item
    {id: "3", name: "X-Men", type: "action"},
    {id: "4", name: "Ordinary People", type: "drama"},
    {id: "5", name: "Billy Elliot", type: "drama"},
    {id: "6", name: "Toy Story", type: "children"}
]};

You can remove some and add some at once:

data.items.splice(1, 3,
    {id: "7", name: "Douglas Adams", type: "comedy"},
    {id: "8", name: "Dick Francis", type: "mystery"}
);

...which means: Starting at index 1, remove three entries, then add these two entries. Which results in:

var data = {items: [
    {id: "1", name: "Snatch", type: "crime"},
    {id: "7", name: "Douglas Adams", type: "comedy"},
    {id: "8", name: "Dick Francis", type: "mystery"}
    {id: "5", name: "Billy Elliot", type: "drama"},
    {id: "6", name: "Toy Story", type: "children"}
]};
Rituraj ratan
  • 10,260
  • 8
  • 34
  • 55