简体   繁体   中英

JSON-LD: Trouble generating triples, without IRI as @id in json doc

I am using https://github.com/jsonld-java/jsonld-java for generating triples from a JSON document. I am new to json-ld and I was wondering is there a way to generate triples with @id being a literal or does @id requires an IRI.?

Sample Json input:

[
  {
"@context": {
  "@vocab": "http://schema.org/",
  "@base": "test.com"
},
"@graph": [
  {
    "@type": [
      "Person",
      "Entity"
    ],
    "@id": "abcdef",
    "personName": "David"
  }
  ]
 }
]

Code Snippet:

JsonLdProcessor.toRDF(obj, options)

for options, I am using the following JsonLd Options config

this.format = "application/nquads"

The above document doesn't produce any triples. When I change the @id field in the above json document to "@id": " http://abcdef ", it generates the following triples.

<http://abcdef> <http://schema.org/personName> "David" _:b0 .

<http://abcdef> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Entity> _:b0 .

<http://abcdef> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> _:b0 .

Any pointers or suggestions would be great.

Since you're talking about NQuads, the subject always need to be either an IRI or a BNode. As the data model for JSON-LD is RDF, that applies more generally to values of @id.

However, it is possible to specify @id using just a string, which will be treated as a path component based on the location of the document or @base in the context. Note that this is a form of an IRI called a relative IRI , so there are syntactic limitations based on the URI/IRI spec. Typically, JSON-LD doesn't care, and will happily generate an illegal IRI. But, if there's no document base, it will silently drop subject nodes.

You do use an @base in your context, but it's not an absolute IRI, so that generates an error. You can turn it into an absolute IRI, using something like http://test.com/ (note the trailing slash). That might be more what you're looking for.

It's also good to try out examples on the JSON-LD playground .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM