Skip to content

JSON-LD Parser adds unnecessary namespaces - Serializer fails to resolve namespace #3373

@J0las

Description

@J0las

Hi,

I am using rdflib to parse json-lds from a database which at a later point will be serialized again.
As part of this I want to ensure that the resulting serialized graph is as close as possible to the original json-ld.
However, I noticed that the serialized graph contains a lot of extra namespace definitions in its context that are unneeded.
Even when setting bind_namespaces to "none" for the namespace manager.
Additionally the namespace https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/ is not mapped back to its prefix mds.

I am running Python 3.10.12 and rdflib 7.5.0

Would this be expected behavior?

Thanks for your support!

MWE:

from rdflib import Graph

source = "path_to_original_jsonld"

graph = Graph(
    store="Memory",
    bind_namespaces="none",
)

print(len(tuple(graph.namespaces()))) # 0

graph.parse(
    source=source
)

print(len(tuple(graph.namespaces()))) # 33

graph.serialize(
    destination="serialized.jsonld",
    format="json-ld",
    auto_compact=True,
)

Original JSON-LD:

{
  "@context": {
    "mds": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/",
    "obo": "http://purl.obolibrary.org/obo/",
    "cco": "https://www.commoncoreontologies.org/",
    "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
    "skos": "http://www.w3.org/2004/02/skos/core#",
    "qudt": "http://qudt.org/schema/qudt/",
    "owl": "http://www.w3.org/2002/07/owl#",
    "xsd": "http://www.w3.org/2001/XMLSchema#",
    "dcterms": "http://purl.org/dc/terms/"
  },
  "@graph": [
    {
      "@id": "mds:XrayResult_re00007-is00002-im0081",
      "@type": "mds:XrayResult",
      "cco:ont00001808": {
        "@id": "mds:sa50002"
      },
      "dcterms:created": {
        "@value": "2018-03-26T20:45:39",
        "@type": "xsd:dateTime"
      }
    },
    {
      "@id": "mds:ResultID_re00007-is00002-im0081",
      "@type": "mds:ResultID",
      "cco:ont00001765": "u-sa50002-si0001-to50001-re00007-is00002-im0081-or0000000205935222-ts20180326204539",
      "cco:ont00001916": {
        "@id": "mds:XrayResult_re00007-is00002-im0081"
      }
    },
    {
      "@id": "mds:re00007",
      "@type": "mds:XrayRecipe",
      "cco:ont00001986": {
        "@id": "mds:XrayResult_re00007-is00002-im0081"
      },
      "obo:BFO_0000057": [
        {
          "@id": "mds:sa50002"
        },
        {
          "@id": "mds:to50001"
        },
        {
          "@id": "mds:ge02"
        }
      ]
    },
    {
      "@id": "mds:ImageFile_re00007-is00002-im0081",
      "@type": "mds:ImageFile",
      "rdfs:label": "ff_011276_ge2_0081.tiff"
    },
    {
      "@id": "mds:ImageStack_re00007-is00002-im0081",
      "@type": "mds:ImageStack",
      "obo:BFO_0000176": {
        "@id": "mds:XrayResult_re00007-is00002-im0081"
      },
      "obo:BFO_0000178": {
        "@id": "mds:DiffractionPattern_re00007-is00002-im0081"
      }
    },
    {
      "@id": "mds:DiffractionPattern_re00007-is00002-im0081",
      "@type": "mds:DiffractionPattern",
      "obo:BFO_0000084": {
        "@id": "mds:ImageFile_re00007-is00002-im0081"
      }
    }
  ]
}

Serialized Graph:

{
  "@context": {
    "brick": "https://brickschema.org/schema/Brick#",
    "cco": "https://www.commoncoreontologies.org/",
    "csvw": "http://www.w3.org/ns/csvw#",
    "dc": "http://purl.org/dc/elements/1.1/",
    "dcam": "http://purl.org/dc/dcam/",
    "dcat": "http://www.w3.org/ns/dcat#",
    "dcmitype": "http://purl.org/dc/dcmitype/",
    "dcterms": "http://purl.org/dc/terms/",
    "doap": "http://usefulinc.com/ns/doap#",
    "foaf": "http://xmlns.com/foaf/0.1/",
    "geo": "http://www.opengis.net/ont/geosparql#",
    "mds": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/",
    "obo": "http://purl.obolibrary.org/obo/",
    "odrl": "http://www.w3.org/ns/odrl/2/",
    "org": "http://www.w3.org/ns/org#",
    "owl": "http://www.w3.org/2002/07/owl#",
    "prof": "http://www.w3.org/ns/dx/prof/",
    "prov": "http://www.w3.org/ns/prov#",
    "qb": "http://purl.org/linked-data/cube#",
    "qudt": "http://qudt.org/schema/qudt/",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
    "schema": "https://schema.org/",
    "sh": "http://www.w3.org/ns/shacl#",
    "skos": "http://www.w3.org/2004/02/skos/core#",
    "sosa": "http://www.w3.org/ns/sosa/",
    "ssn": "http://www.w3.org/ns/ssn/",
    "time": "http://www.w3.org/2006/time#",
    "vann": "http://purl.org/vocab/vann/",
    "void": "http://rdfs.org/ns/void#",
    "wgs": "https://www.w3.org/2003/01/geo/wgs84_pos#",
    "xsd": "http://www.w3.org/2001/XMLSchema#"
  },
  "@graph": [
    {
      "@id": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/re00007",
      "@type": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/XrayRecipe",
      "cco:ont00001986": {
        "@id": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/XrayResult_re00007-is00002-im0081"
      },
      "obo:BFO_0000057": [
        {
          "@id": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/sa50002"
        },
        {
          "@id": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/to50001"
        },
        {
          "@id": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/ge02"
        }
      ]
    },
    {
      "@id": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/DiffractionPattern_re00007-is00002-im0081",
      "@type": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/DiffractionPattern",
      "obo:BFO_0000084": {
        "@id": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/ImageFile_re00007-is00002-im0081"
      }
    },
    {
      "@id": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/ImageFile_re00007-is00002-im0081",
      "@type": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/ImageFile",
      "rdfs:label": "ff_011276_ge2_0081.tiff"
    },
    {
      "@id": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/ImageStack_re00007-is00002-im0081",
      "@type": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/ImageStack",
      "obo:BFO_0000176": {
        "@id": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/XrayResult_re00007-is00002-im0081"
      },
      "obo:BFO_0000178": {
        "@id": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/DiffractionPattern_re00007-is00002-im0081"
      }
    },
    {
      "@id": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/ResultID_re00007-is00002-im0081",
      "@type": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/ResultID",
      "cco:ont00001765": "u-sa50002-si0001-to50001-re00007-is00002-im0081-or0000000205935222-ts20180326204539",
      "cco:ont00001916": {
        "@id": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/XrayResult_re00007-is00002-im0081"
      }
    },
    {
      "@id": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/XrayResult_re00007-is00002-im0081",
      "@type": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/XrayResult",
      "cco:ont00001808": {
        "@id": "https://cwrusdle.bitbucket.io/files/MDS_Onto/index-en.html#https://cwrusdle.bitbucket.io/mds/sa50002"
      },
      "dcterms:created": {
        "@type": "xsd:dateTime",
        "@value": "2018-03-26T20:45:39"
      }
    }
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions