IDigBio API v1 Examples: Difference between revisions

Line 7: Line 7:
=== Viewing JSON Output ===
=== Viewing JSON Output ===


Javascript Object Notation ([http://en.wikipedia.org/wiki/JSON JSON]), is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. JSON has become a common format for interchanging data via APIs and especially modern Web Services. Effective use of the iDigBio API will require some understanding of JSON.
Most of the examples provided here will be shown using the <code>curl</code> command-line tool. The dollar sign <code>$</code> is an indication of a command prompt where something is typed into a shell or terminal. To run the commands, the <code>$</code> should be omitted.


Most of the examples provided here will be shown using the <code>curl</code> command-line tool.  Here are some of the more commonly used curl options (for descriptions and additional usage information, see the curl man page):
Here are some of the more commonly used curl options in our examples (for descriptions and additional usage information, see the curl man page):


<pre>
<pre>
Line 15: Line 15:
...
...
       -s, --silent
       -s, --silent
      -i, --include
       -v, --verbose
       -v, --verbose
      -X, --request <command>
       -d, --data <data>
       -d, --data <data>
...
...
</pre>
</pre>


A short JSON document is returned by the API when calling the HTTP "GET" method (curl's default) against the base URL:
Many of the examples will also include "pipes" to chain multiple commands together. The [http://en.wikipedia.org/wiki/Pipeline_(Unix) Wikipedia article on Unix software pipelines] may be a useful reference.
 
==== Javascript Object Notation (JSON) Quick Intro ====
 
Javascript Object Notation ([http://en.wikipedia.org/wiki/JSON JSON]), is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. JSON has become a common format for interchanging data via APIs and especially modern Web Services. Effective use of the iDigBio API will require some understanding of JSON.
 
A simple JSON document with two fields and two data values would look something like this:


<pre>
<pre>
$ curl http://api.idigbio.org/
{
    "field1" : "value_a",
    "field2" : "value_b"
}
</pre>
 
If we were storing data about John Smith, age 50, we might represent it as the following JSON document:
 
<pre>
{
    "lastName" : "Smith",
    "firstName" : "John",
    "age" : "50"
}
</pre>
 
 
A short JSON document is returned by the iDigBio API when calling the HTTP "GET" method (curl's default) against the base URL (http://api.idigbio.org):


{"v1":"http://api.idigbio.org/v1/","v0":"http://api.idigbio.org/v0/","check":"http://api.idigbio.org/check"}
<pre>
{
  "v1" : "http://api.idigbio.org/v1/",
  "check" : "http://api.idigbio.org/check",
  "v0" : "http://api.idigbio.org/v0/"
}
</pre>
</pre>


Line 38: Line 64:
==== JSONView Web Browser Extension ====
==== JSONView Web Browser Extension ====


For best viewing of JSON results in a browser, one may wish to use the [http://jsonview.com/ JSONView Extension] which is available for Firefox and Chrome.
For viewing of JSON results in a browser, one may wish to use the [http://jsonview.com/ JSONView Extension] which is available for Firefox and Chrome.


Here is a screenshot of the output using the JSONView extension after requesting the same API base URL:
Here is a screenshot of the output using the JSONView extension after requesting the API base URL:


[[File:JSONView_browser_extension_example.png|border|JSONView extension allows easier viewing in a web browser]]
[[File:JSONView_browser_extension_example.png|border|JSONView extension allows easier viewing in a web browser]]
Line 73: Line 99:


If a Python installation is misssing the json standard library, the separate simplejson package may solve the problem.
If a Python installation is misssing the json standard library, the separate simplejson package may solve the problem.
Install the Python simplejson module on Debian / Ubuntu Linux with:
<pre>
$ sudo apt-get install python-simplejson
</pre>
Install the Python simplejson on Red Hat / CentOS Linux with:
<pre>
$ # log in as root or use sudo
$ yum install python-simplejson
</pre>


The simplejson.tool module can be invoked in the same way as json.tool:
The simplejson.tool module can be invoked in the same way as json.tool:
1,554

edits