This notebook demonstrates how to convert a Resource to JSON and vice-versa.
from kgforge.core import KnowledgeGraphForge
forge = KnowledgeGraphForge("../../configurations/demo-forge.yml")
import json
from kgforge.core import Resource
def pp(x): print(json.dumps(x, indent=4))
address = Resource(type="PostalAddress", country="Switzerland", locality="Geneva")
jane = Resource(type="Person", name="Jane Doe", address=address, email="(missing)")
john = Resource(type="Person", name="John Smith", email="john.smith@epfl.ch")
persons = [jane, john]
forge.register(persons)
<count> 2 <action> _register_one <succeeded> True
association = Resource(type="Association", agent=persons)
forge.register(association)
<action> _register_one <succeeded> True
pp(forge.as_json(association))
{ "id": "e5ed50d2-bc03-45c5-9953-4efed4ddfb0d", "type": "Association", "agent": [ { "id": "983b316e-db52-4308-8c7b-979654cab3c8", "type": "Person", "address": { "type": "PostalAddress", "country": "Switzerland", "locality": "Geneva" }, "email": "(missing)", "name": "Jane Doe" }, { "id": "8dd74b04-b9b5-4ad2-a9e0-63251e514ebe", "type": "Person", "email": "john.smith@epfl.ch", "name": "John Smith" } ] }
pp(forge.as_json(jane, store_metadata=True))
{ "id": "983b316e-db52-4308-8c7b-979654cab3c8", "type": "Person", "address": { "type": "PostalAddress", "country": "Switzerland", "locality": "Geneva" }, "email": "(missing)", "name": "Jane Doe", "deprecated": false, "version": 1 }
address = Resource(type="PostalAddress", country="Switzerland", locality="Geneva")
jane = Resource(type="Person", name="Jane Doe", address=address, email="(missing)")
john = Resource(type="Person", name="John Smith", email="john.smith@epfl.ch")
persons = [jane, john]
association = Resource(type="Association", agent=[jane, john])
data = {
"type": "Association",
"agent": [
{
"type": "Person",
"address": {
"type": "PostalAddress",
"country": "Switzerland",
"locality": "Geneva",
},
"email": "(missing)",
"name": "Jane Doe"
},
{
"type": "Person",
"email": "john.smith@epfl.ch",
"name": "John Smith"
}
]
}
resource = forge.from_json(data)
resource == association
True
print(forge.from_json(data, na="(missing)"))
{ type: Association agent: [ { type: Person address: { type: PostalAddress country: Switzerland locality: Geneva } name: Jane Doe } { type: Person email: john.smith@epfl.ch name: John Smith } ] }