1

Hello i am building a web app and i use itunes search api. So when i am searching for a specific track of an artist itunes return to me a JSON with some info related to the search term i put.

The JSON looks like this:

{
 "resultCount":1,
 "results": [
{"wrapperType":"track", "kind":"song", "artistId":271256, "collectionId":1025130620, "trackId":1025130621, "artistName":"Drake", "collectionName":"Hotline Bling - Single", "trackName":"Hotline Bling", "collectionCensoredName":"Hotline Bling - Single", "trackCensoredName":"Hotline Bling", "artistViewUrl":"https://itunes.apple.com/us/artist/drake/id271256?uo=4", "collectionViewUrl":"https://itunes.apple.com/us/album/hotline-bling/id1025130620?i=1025130621&uo=4", "trackViewUrl":"https://itunes.apple.com/us/album/hotline-bling/id1025130620?i=1025130621&uo=4", "previewUrl":"http://a1586.phobos.apple.com/us/r1000/172/Music6/v4/f6/2f/76/f62f7681-0f4c-3a9f-9aeb-2098297652f7/mzaf_7372296458390444020.plus.aac.p.m4a", "artworkUrl30":"http://is1.mzstatic.com/image/thumb/Music7/v4/2d/c5/31/2dc53130-44b8-8347-2f83-bce05ee8a649/source/30x30bb.jpg", "artworkUrl60":"http://is1.mzstatic.com/image/thumb/Music7/v4/2d/c5/31/2dc53130-44b8-8347-2f83-bce05ee8a649/source/60x60bb.jpg", "artworkUrl100":"http://is1.mzstatic.com/image/thumb/Music7/v4/2d/c5/31/2dc53130-44b8-8347-2f83-bce05ee8a649/source/100x100bb.jpg", "collectionPrice":1.29, "trackPrice":1.29, "releaseDate":"2015-07-31T07:00:00Z", "collectionExplicitness":"notExplicit", "trackExplicitness":"notExplicit", "discCount":1, "discNumber":1, "trackCount":1, "trackNumber":1, "trackTimeMillis":267024, "country":"USA", "currency":"USD", "primaryGenreName":"Hip-Hop/Rap", "radioStationUrl":"https://itunes.apple.com/station/idra.1025130621", "isStreamable":true}]
}

How am i able to get a specific value about "artistId" that is inside results array?

I use an $.getJSON call and in the callback i am unable to get this value.

Take a look in this jsfiddle example at the line 54 of javascript

1
  • I need artistId and trackId Commented Feb 1, 2016 at 12:44

2 Answers 2

3

You cant get Id like this

 var arr={
 "resultCount":1,
 "results": [
{"wrapperType":"track", "kind":"song", "artistId":271256, "collectionId":1025130620, "trackId":1025130621, "artistName":"Drake", "collectionName":"Hotline Bling - Single", "trackName":"Hotline Bling", "collectionCensoredName":"Hotline Bling - Single", "trackCensoredName":"Hotline Bling", "artistViewUrl":"https://itunes.apple.com/us/artist/drake/id271256?uo=4", "collectionViewUrl":"https://itunes.apple.com/us/album/hotline-bling/id1025130620?i=1025130621&uo=4", "trackViewUrl":"https://itunes.apple.com/us/album/hotline-bling/id1025130620?i=1025130621&uo=4", "previewUrl":"http://a1586.phobos.apple.com/us/r1000/172/Music6/v4/f6/2f/76/f62f7681-0f4c-3a9f-9aeb-2098297652f7/mzaf_7372296458390444020.plus.aac.p.m4a", "artworkUrl30":"http://is1.mzstatic.com/image/thumb/Music7/v4/2d/c5/31/2dc53130-44b8-8347-2f83-bce05ee8a649/source/30x30bb.jpg", "artworkUrl60":"http://is1.mzstatic.com/image/thumb/Music7/v4/2d/c5/31/2dc53130-44b8-8347-2f83-bce05ee8a649/source/60x60bb.jpg", "artworkUrl100":"http://is1.mzstatic.com/image/thumb/Music7/v4/2d/c5/31/2dc53130-44b8-8347-2f83-bce05ee8a649/source/100x100bb.jpg", "collectionPrice":1.29, "trackPrice":1.29, "releaseDate":"2015-07-31T07:00:00Z", "collectionExplicitness":"notExplicit", "trackExplicitness":"notExplicit", "discCount":1, "discNumber":1, "trackCount":1, "trackNumber":1, "trackTimeMillis":267024, "country":"USA", "currency":"USD", "primaryGenreName":"Hip-Hop/Rap", "radioStationUrl":"https://itunes.apple.com/station/idra.1025130621", "isStreamable":true}]
}
arr.results.forEach(function(i,j){console.log(i.artistId)})
Sign up to request clarification or add additional context in comments.

Comments

1

In the fiddle you've provided, you try to access the artistId, by writing data.results.artistId, however results is an array, and as such you must access a property at one of it's indices. So something like data.results[0].artistId should work for you.

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.