0

I have the following code, parsing the JSON-ld file below. However, it only outputs the top-level triples and not the other, such as the name of the WebPage

from rdflib import Dataset
from rdflib.namespace import Namespace, NamespaceManager

local_input="""
[
    {
        "@context": "https://schema.org/"
    },
    {
        "@type": "WebPage",
        "@id": "https://example.com/glossary/term/",
        "name": "My Glossary Term",
        "abstract": "Just my glossary Term.",
        "datePublished": "2024-03-08T07:52:13+02:00",
        "dateModified": "2024-03-08T14:54:13+02:00",
        "url": "https://example.com/glossary/term/",
        "author": {
            "@type": "Person",
            "@id": "https://example.com/",
            "name": "John Doe"
        },
        "about": [
            {
                "@type": "DefinedTerm",
                "@id": "https://example.com/glossary/term/#definedTerm"
            }
        ]
    },
    {
        "@type": "DefinedTerm",
        "@id": "https://example.com/glossary/term/#definedTerm",
        "name": "My Term",
        "description": "Just my Term",
        "inDefinedTermSet": {
            "@type": "DefinedTermSet",
            "@id": "https://example.com/glossary/#definedTermSet"
        }
    }
]
"""

SCH = Namespace('https://schema.org/')
namespace_manager = NamespaceManager(Dataset(), bind_namespaces='none')
namespace_manager.bind('', SCH, override=True)
g = Dataset()
g.namespace_manager = namespace_manager
g.parse(data=local_input, format='json-ld', publicID="http://schema.org/")

print(len(g))

import pprint
for stmt in g:
    pprint.pprint(stmt)

OUTPUT:

2
(rdflib.term.URIRef('https://example.com/glossary/term/'),
 rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
 rdflib.term.URIRef('http://schema.org/WebPage'),
 rdflib.term.URIRef('urn:x-rdflib:default'))
(rdflib.term.URIRef('https://example.com/glossary/term/#definedTerm'),
 rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
 rdflib.term.URIRef('http://schema.org/DefinedTerm'),
 rdflib.term.URIRef('urn:x-rdflib:default'))
0

1 Answer 1

0

You JSON-LD input data is not quite right, try this:

{
  "@context": {
    "@vocab": "https://schema.org/"
  },
  "@graph": [
    {
      "@id": "https://example.com/glossary/#definedTermSet",
      "@type": "DefinedTermSet"
    },
    {
      "@id": "https://example.com/",
      "@type": "Person",
      "name": "John Doe"
    },
    {
      "@id": "https://example.com/glossary/term/",
      "@type": "WebPage",
      "about": {
        "@id": "https://example.com/glossary/term/#definedTerm"
      },
      "abstract": "Just my glossary Term",
      "author": {
        "@id": "https://example.com/"
      },
      "dateModified": "2024-03-08T14:54:13+02:00",
      "datePublished": "2024-03-08T07:52:13+02:00",
      "name": "My Glossary Term",
      "url": "https://example.com/glossary/term/"
    },
    {
      "@id": "https://example.com/glossary/term/#definedTerm",
      "@type": "DefinedTerm",
      "inDefinedTermSet": {
        "@id": "https://example.com/glossary/#definedTermSet"
      },
      "name": "My Term"
    }
  ]
}

You can load this and it will print out all the triples you tried to create in the data.

Sign up to request clarification or add additional context in comments.

2 Comments

In what sense is the JSON-LD not quite right? I understand if I have no nested elements, such as ``` "author": { "@type": "Person", "@id": "example.com", "name": "John Doe" }, ``` it does not need to read them. Yet it does not explain why RDFLib does not read it.
I think it is to do with the @context part. I think you need to indicate a @vocab key in there for the base namespace of all the other keys in the document and then you also need to indicate a @graph element in the data. There are several ways to formulate JSON-LD but your data doesn't load correctly in RDFLib or in the JavaScipt-based JSON-LD playground (json-ld.org/playground)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.