#!/usr/bin/env python # coding: utf-8 # # JSON-LD Conversions # # This notebook demonstrates how to [convert](https://nexus-forge.readthedocs.io/en/latest/interaction.html#converting) Resources to [JSON-LD](https://json-ld.org) and vice-versa. JSON-LD is a semantic-preserving JSON format allowing to provide identifiers and meaning to JSON keys and values by mean of an added '@context' object. Read the excellent [JSON-LD documentation](https://json-ld.org/learn.html) to learn more about this format. # # A JSON-LD context can be assigned to a [Resource](https://nexus-forge.readthedocs.io/en/latest/interaction.html#resource) in three ways by order of priority: # 1. directly set in the resource using the property `context`: e.g.`jane_resource.context="https://schema.org/docs/jsonldcontext.json"` # 2. set in the `Model` section of the forge configuration file: e.g. # # # ''' # # Model: # # name: RdfModel # # origin: ... # # source: ... # # context: # # iri: "https://schema.org/docs/jsonldcontext.json" # # ''' # # # 3. set by the configured store. A store (such as BlueBrainNexus) can support JSON-LD and add a default JSON-LD context whenever one is not provided. # In[1]: from kgforge.core import KnowledgeGraphForge # A configuration file is needed in order to create a KnowledgeGraphForge session. A configuration can be generated using the notebook [00-Initialization.ipynb](00%20-%20Initialization.ipynb). # In[2]: forge = KnowledgeGraphForge("../../configurations/forge.yml") # # Imports # In[3]: import json # In[4]: from kgforge.core import Resource # In[5]: def pp(x): print(json.dumps(x, indent=4)) # ## Context # In[6]: context = { "ex": "http://example.org/", "Person": "ex:Person", "Organization": "ex:Organization", "employer": "ex:employer", "name": "ex:name" } # ## Resource to JSON-LD # ### context from the user # #### locally defined context # In[7]: jane = Resource(context=context, type="Person", name="Jane Doe") # In[8]: forge.as_json(jane) # In[9]: pp(forge.as_jsonld(jane)) # In[11]: pp(forge.as_jsonld(jane, form="expanded")) # #### remote context from the web # In[12]: jane = Resource(context="https://schema.org/docs/jsonldcontext.json", type="Person", name="Jane Doe") # In[13]: print(jane) # In[14]: pp(forge.as_jsonld(jane)) # ### context from the model # # The configured model provides a default context that will be used to create resources that do not have context provided. # In[15]: jane = Resource(type="Person", name="Jane Doe") # In[16]: print(jane) # In[17]: pp(forge.as_jsonld(jane)) # In[18]: pp(forge.as_jsonld(jane, form="expanded")) # ### context from the Store # # It is possible to use a context that is available in the Store when configured. # In[19]: jane = Resource(context="https://bbp.neuroshapes.org", type="Person", name="Jane Doe") # In[20]: print(jane) # Since this context is not locally resolvable the json-ld conversion will fail. # In[21]: pp(forge.as_jsonld(jane)) # In[22]: pp(forge.as_jsonld(jane, form="expanded")) # In[23]: forge.register(jane) # In[24]: pp(forge.as_jsonld(jane, store_metadata=True)) # ## JSON-LD to Resource # In[25]: john = Resource(context=context, type="Person", name="John Smith") # In[26]: data = { "@context": context, "@type": "Person", "name": "John Smith", } # In[27]: resource = forge.from_jsonld(data) # In[28]: resource == john