ontolutils.classes.thing.Thing.get_jsonld_dict

Thing.get_jsonld_dict(base_uri: Optional[Union[str, AnyUrl]] = None, context: Optional[Union[Dict, str]] = None, exclude_none: bool = True, resolve_keys: bool = False) Dict

Return the JSON-LD dictionary of the object. This will include the context and the fields of the object.

context: Optional[Union[Dict, str]]

The context to use for the JSON-LD serialization. If a string is given, it will be interpreted as an import statement and will be added to the context.

exclude_none: bool=True

Exclude fields with None values

resolve_keys: bool=False

If True, then attributes of a Thing class will be resolved to the full IRI and explained in the context.

base_uri: Optional[Union[str, AnyUrl]]=None

The base URI to use for blank nodes (only used if no ID is set). This is useful, because blank nodes are not globally unique and can lead to problems when merging data from different sources.

Example:

In the following example, first_name refers to foaf:firstName:

>>> @namespaces(foaf='http://xmlns.com/foaf/0.1/')
>>> @urirefs(Person='foaf:Person', first_name='foaf:firstName')
>>> class Person(Thing):
>>>     first_name: str = None
>>> p = Person(first_name='John')
>>> p.model_dump_jsonld(resolve_keys=True)

This will result “first_name”: “foaf:firstName” showing up in the context:

>>> {
>>>     "@context": {
>>>         "foaf": "http://xmlns.com/foaf/0.1/",
>>>         "first_name": "foaf:firstName"
>>>     },
>>>     "@type": "foaf:Person",
>>>     "foaf:firstName": "John"
>>> }

While resolve_keys=False will result in:

>>> {
>>>     "@context": {
>>>         "foaf": "http://xmlns.com/foaf/0.1/"
>>>     },
>>>     "@type": "foaf:Person",
>>>     "foaf:firstName": "John"
>>> }
Dict

The JSON-LD dictionary