JSON Profile Data
JSON is a nice, lightweight format which is relatively terse and provides for a simple way to encode complex information. There are libraries (or native support) for it in most programming languages now, including Javascript, which makes it a perfect cross-language encoding format. Requesting profile data in JSON is a simple process and gives you immediate access to all open profile information.
Base Request
Making a JSON request is likely to follow these steps (assuming you have an email address already):
- Create a valid email hash
- Build the URL to request a profile page
- Append
.json
to that URL to indicate that you want the results in JSON format
Once you have done the above, you should end up with a URL that looks something like this:
https://www.gravatar.com/205e460b479e2e5b48aec07710c08d50.json
When you request that URL (you can do it in your browser) you should get a JSON document containing all open profile data. We use Portable Contacts to format our profile data whereever possible, so see the PoCo spec for more details on the structure of the response.
Request Options
Gravatar also supports JSONP requests to make working with the data via Javascript a little easier. If you append a query string parameter callback
to the URL, then that will be wrapped around the resulting JSON object and thus executed when the data is fully loaded.
A request with a callback (to execute an 'alert') would look something like this:
https://www.gravatar.com/205e460b479e2e5b48aec07710c08d50.json?callback=alert
This makes it easy to load a Gravatar profile dynamically and immediately act on the contents of that profile by just loading a script URL like this:
<script type="text/javascript" src="https://www.gravatar.com/205e460b479e2e5b48aec07710c08d50.json?callback=alert"></script>
Example
Drop the following code into a new file and save it with .html
as the extension, then load it in a browser. It will load my profile and set the title
of the document to my name.
<script type="text/javascript"> function changeTitle( profile ) { document.title = profile.entry[0].displayName; }
Tips
- For security, we recommend always using HTTPS, especially if using callbacks.
- The JSONView plugin for Firefox makes JSON much easier to read.
- The "About Me" or "Description" section of a profile may contain line breaks, so be careful how you use it in Javascript.
- Many fields may contain apostrophes or other characters which affect Javascript output, so be careful to escape those characters if they are a concern.
- Links (a href) within the "Description" field are automatically modified to include
rel="nofollow"
on profile pages, but the raw profile data does not have this change applied. - As with all profile data formats, profile requests will only resolve for the hash of the primary address on an account. Users may have multiple addresses on a single account, so while a Gravatar image request may return a result, a profile request for the same hash may not.