biolink / biolinkml

DEPRECATED: replaced by linkml
https://github.com/linkml/linkml
Creative Commons Zero v1.0 Universal
23 stars 12 forks source link

biolinkml None slots populated in object #297

Closed wdduncan closed 3 years ago

wdduncan commented 3 years ago

This is similar to #296 but pertains to the value None showing up in the objects.

In the nmdc schema, the class named thing is specified like so:

named thing:
    description: "a databased entity or concept/class"
    abstract: true
    slots:
      - id
      - name
      - description
      - alternate identifiers

If I create a simple object like so:

In [1]: import nmdc
In [2]: n1 = nmdc.NamedThing(id='id:1',  name='object 1')

The description slot has the value None:

In [3]: n1
Out[3]: NamedThing(id='id:1', name='object 1', description=None, alternate_identifiers=[])

This results in null values being saved when serializing the object as json. E.g.:

In [5]: with open('test-dump.json', 'w') as f:
    ...:     json.dump(n1.__dict__, f)

produces:

{"id": "id:1", "name": "object 1", "description": null, "alternate_identifiers": []}

Do we want remove the null valued slots? That is, produce the output:

{"id": "id:1", "name": "object 1"}
hsolbrig commented 3 years ago

You need to use biolinkml.utils.yamlutils.as_json_obj() to generate the JSON. Its job, among several things, is to remove all non-initialized types. As an example, see the section titled "and can be combined w/ the JSON-LD Context to generate RDF" in https://github.com/biolink/biolinkml/blob/master/notebooks/examples.ipynb

hsolbrig commented 3 years ago

So, in your case:

     with open('test-dump.json', 'w') as f:
               json.dump(as_json_obj(n1, f))
wdduncan commented 3 years ago

Thanks!