digitalbazaar / jsonld.js

A JSON-LD Processor and API implementation in JavaScript
https://json-ld.org/
Other
1.66k stars 195 forks source link

How to add a type definition to an array of objects? #307

Closed rmeissn closed 4 years ago

rmeissn commented 5 years ago

I've read most parts of https://w3c.github.io/json-ld-syntax/ and a lot on stackoverflow, but still have no idea how to add classes to a list of objects. Hence I have the following json:

{
"@id": "http://ex.org/1",
"answers": [
    {
      "answerText": "Answer 1",
      "points": 3
    },
    {
      "answerText": "Answer 2",
      "points": 0
    }
  ]
}

and this context:

"@context": {
  "@version": 1.1,
  "points": {"@id": "http://ex.org/points", "@type": "http://www.w3.org/2001/XMLSchema#short"},
  "answers": {"@id": "http://ex.org/hasAnswer", "@type": "http://ex.org/Answer", "@container": "@set"},
  "answerText": {"@id": "http://ex.org/hasAnswerText", "@type": "http://www.w3.org/2001/XMLSchema#short"}
},

These resolve to:

<http://ex.org/1> <http://ex.org/hasAnswer> _:b0 .
<http://ex.org/1> <http://ex.org/hasAnswer> _:b1 .
_:b0 <http://ex.org/hasAnswerText> "Answer 1"^^<http://www.w3.org/2001/XMLSchema#short> .
_:b0 <http://ex.org/points> "3"^^<http://www.w3.org/2001/XMLSchema#short> .
_:b1 <http://ex.org/hasAnswerText> "Answer 2"^^<http://www.w3.org/2001/XMLSchema#short> .
_:b1 <http://ex.org/points> "0"^^<http://www.w3.org/2001/XMLSchema#short> .

which is correct. But I also want the result to contain:

 _:b0 a <http://ex.org/Answer> .
 _:b1 a <http://ex.org/Answer> .

like I tried to define in the context. So each answer object shall get the same class definition. I guess this is possible by using node type indexing, but it requires me to alter the json schema (which I don't want). Is there any way I achieve the desired result without altering the actual json (e.g. by a definition in the "@context")?

Vehnem commented 5 years ago

Without altering i have no idea

{
  "@context": {
  "@version": 1.1,
    "points": { "@id": "http://ex.org/points", "@type": "http://www.w3.org/2001/XMLSchema#short"},
    "answers": { "@id": "http://ex.org/hasAnswer", "@container": "@set"},
    "answerText": { "@id": "http://ex.org/hasAnswerText", "@type": "http://www.w3.org/2001/XMLSchema#short"},
    "ex" : "http://ex.org/" 

},
"@id": "http://ex.org/1",
"answers": [
    {
      "answerText": "Answer 1",
      "points": 3,
      "@type" : "ex:Answer"
    },
    {
      "answerText": "Answer 2",
      "points": 0,
      "@type" : "ex:Answer"
    }
  ]
}
davidlehn commented 5 years ago

It's better to bring up issues like this in the JSON-LD syntax spec issue tracker since it's an issue for other implementations too. This type of request has been brought up before in the CG and now the 1.1 WG. I think it was decided to not add this sort of functionality at this time. And more generally to not add features that add data during processing. Adding a @type to everything in an array is the common use case, but if a feature like that exists it should probably handle more than just @type. And pretty soon you have a feature design complexity issue. Feel free to bring this up, but it may not be addressed until a future spec. In the meantime you might look into your own custom processing step to add data.

dlongley commented 4 years ago

Closing per @davidlehn's comments.