JSON DB Python tutorial
JSON DB API quick start tutorial for Python programmers
This tutorial shows how to use the FairCom JSON Action API to interact with a FairCom server. It includes JSON actions in the Admin API for logging in and the DB API for managing database records.
It shows how to POST JSON to the FairCom endpoint. Global variables are used to simplify the code and emphasize that the only reusable code is the function post_json()
.
Note
The <root>
directory for this driver is <faircom>\drivers\python.json.db\
Install and start up the FairCom server if it is not currently running.
Optionally read more about the JSON DB API actions.
Get started immediately with the source code found in the
<root>\tutorials
directory, or proceed with the following instructions (recommended).
The tutorial code performs the following tasks:
Logs into the FairCom server using the
createSession
action.Checks for the existence of the database and creates it if absent.
Checks for the existence of the table within that database and deletes it if present.
Creates the table.
Creates three indexes: two traditional and one that is filtered.
Inserts 6 records.
Prints all records.
Prints the records from the filtered index.
The main()
method contains variables such as the username, password, and table name. It calls methods that perform specific actions, such as login()
and create_table()
. Those methods are designed not to be portable but to show how to accomplish the same objectives.
Most of the methods prepare data to be sent to the FairCom server using HTTP POST. For example, logging in to the server requires the JSON to contain the username and password. Therefore, the login()
method formats the JSON correctly and then passes it to the post_json()
method.
The post_json()
method takes whatever JSON it was provided and POSTs it to the FairCom endpoint. It also performs some basic handling of HTTP errors and FairCom errors.
This tutorial requires the requests
and the urllib3
packages. These requirements are included in the <root>\requirements.txt
file. Use the following command to install them:
pip install -r requirements.txt
Execute the tutorial with this command:
python python.json.tutorial.py
A typical run will look like this:
This program will show how to use the JSON DB API in Python. Creating database 'dev_lane' Creating table 'athlete' in database 'dev_lane' Printing every record in 'athlete': { "birthDate": "1940-10-23", "changeId": 2513800, "earnings": 115000000.0, "favoriteSaying": "Everything is practice.", "id": 4, "livedPast2000": true, "name": "Pele", "playerNumber": 10.0, "ranking": 4 } { "birthDate": "1963-02-17", "changeId": 2513800, "earnings": 1700000000.0, "favoriteSaying": "There is no 'i' in team but there is in win.", "id": 1, "livedPast2000": true, "name": "Michael Jordan", "playerNumber": 23.0, "ranking": 1 } { "birthDate": "1895-02-06", "changeId": 2513800, "earnings": 800000.0, "favoriteSaying": "Every strike brings me closer to the next home run.", "id": 2, "livedPast2000": false, "name": "Babe Ruth", "playerNumber": 3.0, "ranking": 2 } { "birthDate": "1961-01-26", "changeId": 2513800, "earnings": 1720000.0, "favoriteSaying": "You miss 100 percent of the shots you never take.", "id": 5, "livedPast2000": true, "name": "Wayne Gretzky", "playerNumber": 99.0, "ranking": 5 } { "birthDate": "1942-01-17", "changeId": 2513800, "earnings": 60000000.0, "favoriteSaying": "Float like a butterfly, sting like a bee.", "id": 3, "livedPast2000": true, "name": "Muhammad Ali", "playerNumber": 1.0, "ranking": 3 } { "birthDate": "1969-01-03", "changeId": 2513800, "earnings": 990000000.0, "favoriteSaying": "Once something is a passion, the motivation is there.", "id": 6, "livedPast2000": true, "name": "Michael Schumacher", "playerNumber": 1.0, "ranking": 6 } Using the index 'name_livedpast2000' to print every athlete who lived past the year 2000 and was ranked 3rd or higher: { "name": "Muhammad Ali", "ranking": "3" } { "name": "Michael Jordan", "ranking": "1" } Finished.