One of the ways you're most likely to encounter JSON data,
is through the use of a web service. A web service
is essentially a database that you can access using HTTP requests.
With a web service, you formulate queries as URLs. So I'd
like to take a look at a simple example using the
musicbrainz.org site. The nice thing about this site is that we
don't need an API key, so it'll be very easy for
you to experiment with the code I'm going to give you. Okay,
MusicBrainz is essentially a wiki, but one that
provides a web service with access to their data.
The type of data that MusicBrainz maintains, is
metadata on music. So we can ask questions about
artists, labels, recordings, etcetera. Now the way that
we query this site, is by constructing a URL
that has musicbrainz.org as a base, but then specifies
a particular entity, the type of data we're interested
in getting back. And we can specify some additional parameters
that allow us to be specific about exactly what features or
what meta data we'd like to get back for a given
entity, say for an artist. Now, one thing that we're going to
have to do in order to use this site is,
if we want to get specific information about an artist. We
actually need to know its unique identifier, that is the MusicBrainz's
unique identifier. And this theme of unique identifiers is something that's
going to come up over and over and over again in
this course in a variety of different ways. So the first
thing that we're going to do, is actually use their Search interface
and we're just going to pass in a query. That includes some, a
search for the artists we're interested in finding some information about.
We're going to process the results that come back from that query in
order to get the id for that particular artist and then
we can request information about a specific artist using that id. Now,
unless you think I'm a one-trick pony, we're actually going to
ask for data for a different artist, in this case
Lucero. So, here's where we're doing our first query. And
this is going to give us back a results set. Now,
we're going to process that results set in order to get
back the id for Lucero, the MusicBrainz's id for Lucero. Now,
I'm not going to go into this in too much detail.
But something I want to point out here is that, what's happening
is, when we issue this query, the response we get back is JSON data. Using the
JSON module in Python, we can read in
that data and it will simply be translated into
the appropriate Python objects. Now, a JSON object
is equivalent to a Python dictionary. So, what we're
accessing right here is, that Python dictionary and
we're going to access the artists field of that dictionary.
That field happens to have an array as its
value and I happen to know that the second object
in that array is the one I'm actually interested
in, the band Lucero from Memphis, okay. Now we want
the id for this particular artist. The value of
an artist is another object. It was a JSON object
and the results we got back from MusicBrainz in
our case, because we're working in Python, the JSON module
has translated it into another Python dictionary. So,
the id field contains the value we're actually interested
in getting here. Okay, then what we're going to
do is query this side again, this time specifically
requesting information on this particular id, the id that
we got back from our first query. Well, I
should say from processing our first query. Alright. So,
then we're going to issue this second query. We're actually
going to ask for releases. And you can look above in
the code here, to see what's going on here. Essentially, what
I've done is implemented some convenience code that allows us to
easily pass in the parameters that MusicBrainz is expecting in order
to get the releases data back. Okay. And then the
rest of this is simply printing out the results. So, let's
take a look at, what the results look like. So, here's
the result of having dived into the result set from that
first query to get the artists we're interested in. Okay, here's the
id we care about. And then, if we look down, a couple
of things I want to point out here. I've set up this code
so that each time it makes a request out to this web service,
it prints out the URL, right. Again, remember that URL is how
we specify our query to a web service. So, at musicbrainz.org, I'm
specifying that I want to access the web service, version two of the
web service. I believe at least that's what this two means. And then
I'm saying the entity I'm interested in is artist, and
here's the id for that artist. Now, what I want
you to do is give me back data that's formatted
as JSON and this particular parameter for MusicBrainz is where we
specify exactly what metadata we'd like to receive in response.
It's kind of a catch all for everything. If we
want more than one type of metadata, we simply concatenate
them together, here. In this case, I'm just interested in releases,
okay? So then what I did in the code was,
I actually processed the response that I got back to
this query, to pull out the very first release, okay?
And here is the object in Python that's going to be
a dictionary. That represents or that stores the data for
that first release. This happens to be a release entitled 2012-04-20
Webster Hall, New York. As it turns out, I was
actually at this show. My wife had surprised me by flying
in my best friend, who is also a big Lucero
fan. Okay? So then what I did was, in here, I
used a list comprehension to process all of the releases,
and extract just the title for each one of those releases.
And then I'm simply printing them all out here. So
what I encourage you to do with this code, is take
a look at all of it, get a sense for how
it works, and then experiment on your own with some queries.
You can look for different artists, you can use some
of the conveniences that we've provided. Here to query for different
types of metadata about a given artist, okay? So just
play with this part of the code or work with Python
in the Python interpreter, and just get a sense for
the data that's here. What I really encourage you to do,
is print out the full results set, dive into exactly what
type of data is coming back. It's all just hierarchical database
structure that has objects or arrays nested within other objects
or arrays. Following this, we'll have a quiz where we
actually ask you to answer some specific questions, so you
will have to be able to query this web service correctly.