Skip to content

Reading and modifying JSON compliant with API 3.0

Giacomo Marchioro edited this page Jun 18, 2021 · 2 revisions

pyIIIFpres offers some support for reading and modifying JSON files compliant with API 3.

NOTE: pyIIIFpres assumes that the JSON file is compliant with API 3, inspect() and show_errors_in_browser() will highlight only errors in the new addition of the user to the file.

read_API3_json maps most of the IIIF type to IIFpapi3 classes, you can access and modify the elements using the normal set_ methods:

from IIIFpres.utilities import read_API3_json 
mymanifest = read_API3_json('tests/integration/fixtures/0001-mvm-image.json')
mymanifest.items[0].items[0].items[0]
# Out: Annotation id:https://iiif.io/api/cookbook/recipe/0001-mvm-image/annotation/p0001-image
mymanifest.items[0].items[0].items[0].set_id('http:mynewid.com')
mymanifest.items[0].items[0].items[0]
# Out: Annotation id:http:mynewid.com
mymanifest.json_save("revised_manifest.json")

If performance is critical modify_API3_json map only the first IIIF type encountered to IIFpapi3 classes leaving all the rest as dicts.

from IIIFpres.utilities import modify_API3_json
mymanifest = modify_API3_json('tests/integration/fixtures/0001-mvm-image.json')
canvas = mymanifest.add_canvas_to_items()
canvas.set_id(extendbase_url=["canvas","p1"])
canvas.add_label("en","Forgotten painting")
canvas.set_height(1271)
canvas.set_width(2000)
mymanifest.json_save("revised_manifest.json")

The deletion of an IIIF type instance can be done using read_API3_json or modify_API3_json and removing the entity from the structure manually:

mymanifest.items.pop(0)

or using the delete_object_byID if the instance has an ID:

from IIIFpres.utilities import delete_object_byID
mymanifest = modify_API3_json('tests/integration/fixtures/0001-mvm-image.json')
mymanifest.__dict__
delete_object_byID(mymanifest,id='https://iiif.io/api/cookbook/recipe/0001-mvm-image/page/p1/1')
mymanifest.__dict__

NOTE: any nested object will be deleted when deleting the parent object!

If you need supports for reading annotations of both IIIF API 2.1 and 3.0 an excellent package to use could be : python-iiif-annotation-tool