Skip to Content
Author's profile photo Paul Todd

Using $format and $expand

We now up the pace a bit and have a look at some of the more advanced OData System Query Options.

 

How do I change the format I get data in?

 

The format your data is returned in is dictated in one of two ways, either using the “accept” header value in the http request header section and then if that is not present then use the $format system query option. If both the header value and the $format is omitted then it is up to the server to choose, though the normal default value is XML. The recommended way is to set the accept header first, then if that is not possible use the $format option, however be careful if an accept header is specified and a $format system directive then the $format will be the format that wins!

 

The behaviour of this system directive is not standard however.

  • If the $format is omitted from the URL the behaviour will generally be to return the results in XML.
  • If the directive specifies JSON and the server supports returning JSON then the results will be returned in JSON.
  • If the directive specifies XML or ATOM then the behaviour should be to return XML or ATOM data if the server supports it, however in the case of netflix this returns an error.

 

For example omitting the $format

 

http://odata.netflix.com/v2/Catalog/Titles?$top=1

 

will return the data in XML

 

 

However by adding a $format directive

 

http://odata.netflix.com/v2/Catalog/Titles?$top=1&$format=json

 

the data will be returned as JSON

 

 

How do I return the complex types with the entity?

Orthoginal to the $select system query option, the $expand system query option expands related entities inline thus increasing the amount of data to download at the expense of bandwidth. To reduce the number of round trips to the server, it is often useful to be able to get not only the entity in the collection but also some of the other entities directly associated with the entity in question by virtue of the entities navigation property.

 

As an aside, the $expand is the closest OData comes to a join, in particular the LEFT JOIN DML command.

 

 

For example in our netflix sample we can get a movie and all the languages that the movie is avaliable in, via a query like so

 

http://odata.netflix.com/v2/Catalog/Titles?$top=1&$skip=10&$expand=Languages

 

Note that the $skip portion is here just to find a film that is avaliable in multiple languages (in this case english and french) and then the output will look like:

 

  …..

<entry>

    <id>http://odata.netflix.com/v2/Catalog/Titles(’13kag‘)</id>

    <title type=”text”>Meet Joe Black</title>

….

<link rel=”http://schemas.microsoft.com/ado/2007/08/dataservices/related/Languages” type=”application/atom+xml;type=feed” title=”Languages” href=”Titles(’13kag’)/Languages”>

      <m:inline>

        <feed>

          <title type=”text”>Languages</title>

          <id>http://odata.netflix.com/v2/Catalog/Titles(’13kag’)/Languages</id>

          <updated>2013-03-12T11:31:03Z</updated>

          <link rel=”self” title=”Languages” href=”Titles(’13kag’)/Languages” />

          <entry>

            <id>http://odata.netflix.com/v2/Catalog/Languages(‘English‘)</id>

            <title type=”text”>English</title>

            <updated>2013-03-12T11:31:03Z</updated>

            <author>

              <name />

            </author>

            <link rel=”edit” title=”Language” href=”Languages(‘English’)” />

            <link rel=”http://schemas.microsoft.com/ado/2007/08/dataservices/related/Titles” type=”application/atom+xml;type=feed” title=”Titles” href=”Languages(‘English’)/Titles” />

            <category term=”Netflix.Catalog.Shared.Language” scheme=”http://schemas.microsoft.com/ado/2007/08/dataservices/scheme” />

            <content type=”application/xml”>

              <m:properties>

                <d:Name>English</d:Name>

              </m:properties>

            </content>

          </entry>

          <entry>

            <id>http://odata.netflix.com/v2/Catalog/Languages(‘French‘)</id>

            <title type=”text”>French</title>

            <updated>2013-03-12T11:31:03Z</updated>

            <author>

              <name />

            </author>

            <link rel=”edit” title=”Language” href=”Languages(‘French’)” />

            <link rel=”http://schemas.microsoft.com/ado/2007/08/dataservices/related/Titles” type=”application/atom+xml;type=feed” title=”Titles” href=”Languages(‘French’)/Titles” />

            <category term=”Netflix.Catalog.Shared.Language” scheme=”http://schemas.microsoft.com/ado/2007/08/dataservices/scheme” />

            <content type=”application/xml”>

              <m:properties>

                <d:Name>French</d:Name>

              </m:properties>

            </content>

          </entry>

        </feed>

      </m:inline>

    </link>

….

  <content type=”image/jpeg” src=”http://cdn-0.nflximg.com/images/1346/2001346.jpg” />

    <m:properties xmlns:m=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata” xmlns:d=”http://schemas.microsoft.com/ado/2007/08/dataservices“>

      <d:Id>13kag</d:Id>

      <d:Name>Meet Joe Black</d:Name>

      <d:ShortName>Meet Joe Black</d:ShortName>

….

 

 

Notice how the languages association has been implemented as links in the entity so it is easy for callers to navigate from the record to associated records and also how easy it is to return the associated records in one transaction cutting the number of round trips and corresponding data loads.

 

The final blog post in this series will cover the $filter and the $links system query options then we can move onto building simple applications in various Javascript frameworks.

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Paul

      It appears Netflix retired their OData catalog:

      http://odata.netflix.com/

      Are you aware by chance of an alternative OData site to get my head around the OData APIs?

      Thanks, Ralph