JSON parsing help


#1

I have been thinking of creating a script that could utilize the iTunes Search API.
Unfortunately, it appears that my dreams do not match my abilities.
Can anyone help me with parsing out the name/value pairs of the JSON string?
No matter what I have tried thus far all I get out of the findinline command is {"wrapperType".

Here is an example of the JSON string I am trying to parse....

{
 "resultCount":50,
 "results": [
{"wrapperType":"track", "kind":"music-video", "artistId":35934741, "trackId":303126338, "artistName":"Sirenia", "trackName":"The Path to Decay", "trackCensoredName":"The Path to Decay", "artistViewUrl":"http://itunes.apple.com/us/artist/sirenia/id35934741?uo=4", "trackViewUrl":"http://itunes.apple.com/us/music-video/the-path-to-decay/id303126338?uo=4", "previewUrl":"http://a655.v.phobos.apple.com/us/r1000/040/Video/ba/9b/65/mzm.upniplhb..640x360.h264lc.u.p.m4v", "artworkUrl30":"http://a5.mzstatic.com/us/r1000/037/Video/fb/3d/70/mzi.sptytgbv.40x30-75.jpg", "artworkUrl60":"http://a3.mzstatic.com/us/r1000/037/Video/fb/3d/70/mzi.sptytgbv.80x60-75.jpg", "artworkUrl100":"http://a1.mzstatic.com/us/r1000/037/Video/fb/3d/70/mzi.sptytgbv.100x100-75.jpg", "collectionPrice":1.99, "trackPrice":1.99, "releaseDate":"2009-04-07T07:00:00Z", "collectionExplicitness":"notExplicit", "trackExplicitness":"notExplicit", "trackTimeMillis":212280.0, "country":"USA", "currency":"USD"] "primaryGenreName":"Rock"}, 
}

Any assistance or insight would be greatly appreciated!


Any plans to add support for m-TAGS in a future release?
#2

Are you sure, that your example string is a valid JSON string?
Better you offer the entire running websource script, with the not working JSON part commented out.
If you have the chance to use other API, which returns XML, probably Mp3tag websource scripting language can help better.

To get ... ARTIST=Sirenia ... try out for yourself ...

$regexp('"artistName":"Sirenia"','"artistName":"(.+?)"','$1') $regexp(%JSON%,'^.*[{,]"artistName":"(.+?)"[,}].*$','$1')

DD.20111219.0830.CET


#3

It's easy but lots of escaping is needed
findline "wrapperType""
findinline ""artistName":""
sayuntil """


#4

Thanx Dano ... your advice was quite helpful in resolving that issue. But I am still not getting the results I would expect. I have limited my JSON response string to 2 entries for testing purposes but I can not find a way for the script to loop through those entries and populate the results for user selection. Basically, all the script is doing is reading the first JSON object and searching for that in iTunes. I have tried to do...while loop to no avail. Is there a way to define an array and iterate through it in the scripting?

Also, since the JSON only captures specific information I need a way to capture the Artist ID from JSON and then use that to dynamically alter the IndexURL for the artist specific page. Is that possible?

Clearly, I am confused here and appreciate all that you can do to direct me to a solution.

Here is the script I have so far.....

[Name]=iTunes JSON Parser
[BasedOn]=http://itunes.apple.com
[IndexUrl]=http://itunes.apple.com/search?term=%s&entity=album&attribute=artistTerm&limit=2
[AlbumUrl]=http://itunes.apple.com
[WordSeperator]=+
[IndexFormat]=%_url%|%Artist%|%Album%
[SearchBy]=%artist%
[Encoding]=utf-8

[ParserScriptIndex]=...
############################
#         SEARCH RESULTS DIALOG             #
############################
debug "on" "appdata\roaming\mp3tag\itunes.txt"

# ARTIST URL
     findline "wrapperType\""
     findinline "\"artistViewUrl\":\""
     sayuntil ","
     say "|"

#Artist
     findline "wrapperType\""
     findinline "\"artistName\":\""
     sayuntil "\""
     say "|"

# Album
     findline "wrapperType\""
     findinline "\"collectionName\":\""
     sayuntil "\""
     say "|"

#    saynewline

[ParserScriptAlbum]=...
############################
#  ALBUM/TAG CONFIRMATION DIALOG    #
############################
debug "on" "appdata\roaming\mp3tag\itunes.txt"

#Artist
     outputto "Artist"
     findline "wrapperType\""
     findinline "\"artistName\":\""
     sayuntil "\""

#Album
     outputto "Album"
     findline "wrapperType\""
     findinline "\"collectionName\":\""
     sayuntil "\""
    

writeoutput "appdata\roaming\mp3tag\itunes.txt"

And here is the JSON response info that I am using for the testing (I sent it through a validator for readability):

{
   "resultCount":2,
   "results":[
      {
         "wrapperType":"collection",
         "collectionType":"Compilation",
         "artistId":32317,
         "collectionId":391453,
         "amgArtistId":64591,
         "artistName":"Cinderella",
         "collectionName":"20th Century Masters - The Millennium Collection: The Best of Cinderella",
         "collectionCensoredName":"20th Century Masters - The Millennium Collection: The Best of Cinderella",
         "artistViewUrl":"http://itunes.apple.com/us/artist/cinderella/id32317?uo=4",
         "collectionViewUrl":"http://itunes.apple.com/us/album/20th-century-masters-the-millennium/id391453?uo=4",
         "artworkUrl60":"http://a5.mzstatic.com/us/r1000/057/Features/a9/a5/3c/dj.rjjqxwcu.60x60-50.jpg",
         "artworkUrl100":"http://a3.mzstatic.com/us/r1000/057/Features/a9/a5/3c/dj.rjjqxwcu.100x100-75.jpg",
         "collectionPrice":9.99,
         "collectionExplicitness":"notExplicit",
         "trackCount":12,
         "copyright":"2000 The Island Def Jam Music Group",
         "country":"USA",
         "currency":"USD",
         "releaseDate":"2000-08-15T07:00:00Z",
         "primaryGenreName":"Rock"
      },
      {
         "wrapperType":"collection",
         "collectionType":"Album",
         "artistId":32317,
         "collectionId":41419707,
         "amgArtistId":64591,
         "artistName":"Cinderella",
         "collectionName":"Rocked, Wired & Bluesed: The Greatest Hits",
         "collectionCensoredName":"Rocked, Wired & Bluesed: The Greatest Hits",
         "artistViewUrl":"http://itunes.apple.com/us/artist/cinderella/id32317?uo=4",
         "collectionViewUrl":"http://itunes.apple.com/us/album/rocked-wired-bluesed-the-greatest/id41419707?uo=4",
         "artworkUrl60":"http://a4.mzstatic.com/us/r1000/002/Features/c6/f9/aa/dj.brpwqede.60x60-50.jpg",
         "artworkUrl100":"http://a4.mzstatic.com/us/r1000/002/Features/c6/f9/aa/dj.brpwqede.100x100-75.jpg",
         "collectionPrice":9.99,
         "collectionExplicitness":"notExplicit",
         "trackCount":17,
         "copyright":"2005 The Island Def Jam Music Group",
         "country":"USA",
         "currency":"USD",
         "releaseDate":"2005-01-25T08:00:00Z",
         "primaryGenreName":"Rock"
      }
   ]
}

#5

Thanx for the info DetLevD ... Yes the JSON string is a valid one. I added the script as I have it right now in my reply to Dano. And, if I could find an XML for iTunes I would certainly rather use that but it appears they have moved all of the db info into JSON.


#6

This let's you loop through all entries:

[ParserScriptIndex]=...
findline "wrapperType\""

do
	replace "\\\"" """
	# ARTIST URL
	findline "wrapperType\""
	findinline "\"artistViewUrl\":\""
	sayuntil ","
	say "|"

	#Artist
	findline "wrapperType\""
	findinline "\"artistName\":\""
	sayuntil "\""
	say "|"

	# Album
	findline "wrapperType\""
	findinline "\"collectionName\":\""
	sayuntil "\""
	
	saynewline
	
	findline "wrapperType\"" 2 1
	
while "{\"wrapperType" 99

#7

Just as an FYI / template, if you download the most recent version of the IMDB Web Scripts, within it is also a script that parses Apple's response... In the script, you'll see that it is used to retrieve the Apple ID for the movie...

/t/9299/1