Skip to content

Repository files navigation

jsonbox-python

Python wrapper for https://jsonbox.io (with support for V2 features)

PyPI versionLicense: MITDownloadsDownloadsCircleCI

Installation

pip install jsonbox

Usage

fromjsonboximportJsonBox# generate unique box idMY_BOX_ID=JsonBox.get_new_box_id()
# create instancejb=JsonBox()
data= [{"name": "first", "age": 25}, {"name": "second", "age": 19}]
# write dataresult=jb.write(data, MY_BOX_ID)
# get record id of written datarecord_ids=jb.get_record_id(result)
# read recordprint(jb.read(MY_BOX_ID, record_ids[0]))
# read all records in boxprint(jb.read(MY_BOX_ID))
# get metadata for boxprint(jb.get_meta(MY_BOX_ID))
# read all records in box with sortprint(jb.read(MY_BOX_ID, sort_by="age"))
# read records in box with sort matching query (see documentation for syntax)print(jb.read(MY_BOX_ID, query="name:firs*"))
print(jb.read(MY_BOX_ID, query="age:=19"))
# read records with limitprint(jb.read(MY_BOX_ID, limit=1))
# read records with skipprint(jb.read(MY_BOX_ID, skip=1))
# update datadata= {"name": "Bob", "age": 23}
jb.update(data, MY_BOX_ID, record_ids[0])
# read updated dataprint(jb.read(MY_BOX_ID))
print(jb.read(MY_BOX_ID, record_ids[0]))
# delete records matching to queryprint(jb.delete(MY_BOX_ID, query="age:=23"))
# delete recordsjb.delete(MY_BOX_ID, record_ids[1])
# write to a private boxMY_PRIVATE_BOX_ID=JsonBox.get_new_box_id()
api_key=jb.get_new_api_key()
result=jb.write(data, MY_PRIVATE_BOX_ID, api_key=api_key)
record_id=jb.get_record_id(result)
# update a private boxdata= {"name": "David", "age": 35}
jb.update(data, MY_PRIVATE_BOX_ID, record_id, api_key=api_key)
# delete a private boxjb.delete(MY_PRIVATE_BOX_ID, record_id, api_key=api_key)

Private Box

Add api_key to write/update/delete as shown above.
read is always accessible, even with out an api_key.
A valid and unique api_key can be generated using get_new_api_key()

Query Params

As supported (and documented) by https://github.com/vasanthv/jsonbox

You can query by constructing a query string and passing it to the query parameter:

name:arya%20stark,age:>13

The above sample will look for the name arya stark and age greater than 13.

You can filter on Number, String & Boolean values only.

Filters for Numeric values.

Sample
To filter values greater than or less than a specific valueage:>10 or age:<10
To filter values greater (or less) than or equal to a specific valueage:>=10 or age:<=10
To filter values that match a specific value.age:=10

Filters for String values.

Sample
Filter values that start with a specific stringname:arya*
Filter values that end with a specific stringname:*stark
Filter values where a specific string appears anywhere in a stringname:*ya*
Filter values that match a specific stringname:arya%20stark

You can combine multiple fields by separating them with commas as shown below:

name:arya%20stark,age:>13,isalive:true

Deleting records

There are two ways to delete records (see examples above for both)

  • Provide the record_id
  • Provide a query parameter, with the same syntax used for filtering on read

Use your own instance of jsonbox

jb = JsonBox(service_host="https://my-jsonbox-server.com")

License

MIT

Releases

Packages

Used by

Contributors

Languages