json-ld / json-ld.org

JSON for Linked Data's documentation and playground site
https://json-ld.org/
Other
853 stars 152 forks source link

Why is an @id not allowed on @value? #744

Closed davidbarratt closed 3 years ago

davidbarratt commented 3 years ago

When I try to use an @id on a "value object" I get the error in the playground:

jsonld.SyntaxError: Invalid JSON-LD syntax; an element containing @value may only have an @index property and either @type or either or both @language or @direction.

I looked through the spec and that is indeed consistent, but I suppose I don't understand why. I was under the impression that @value represented rdf:value is that not the case? If so (or something similar), shouldn't the object also allow for an (optional) id?

The reason I'm trying to do this is that I have scalar values that will also have ids. I suppose I could construct an object with an rdf:value property, but that seemed like overkill when JSON-LD has this representation already?

Thanks for your help!

gkellogg commented 3 years ago

When I try to use an @id on a "value object" I get the error in the playground:

jsonld.SyntaxError: Invalid JSON-LD syntax; an element containing @value may only have an @index property and either @type or either or both @language or @direction.

I looked through the spec and that is indeed consistent, but I suppose I don't understand why. I was under the impression that @value represented rdf:value is that not the case? If so (or something similar), shouldn't the object also allow for an (optional) id?

No, there is no real correlation with rdf:value. @value is used in an object to indicate that it represents a literal value, so it can't also have an @id. The only other keys that can be used in a value object are @type, to specify the datatype of the literal, @language, to represent the language, @direction (introduced in JSON-LD) to represent the text direction, and @index, for data indexing, or of course aliases of those keys.

The reason I'm trying to do this is that I have scalar values that will also have ids. I suppose I could construct an object with an rdf:value property, but that seemed like overkill when JSON-LD has this representation already?

The use of rdf:value in RDF is also fairly rare IMO, but certainly consistent. I'm not sure what you're considering scalar values (if not literals), but schema.org often relies on WikiData for enumeration values, where those values are entities, which could be represented using node objects.

davidbarratt commented 3 years ago

Ah. Thanks for the explanation.

I suppose what I'm really asking is: Is there a way to have a "value object" that has an identifier?

Basically I want something like this:

{
  "@id": "http://example.com/item/value/1234",
  "@value": "Hello!",
  "@language": "en"
}

I guess I could do something like this?

{
  "@id": "http://example.com/item/value/1234",
  "@type": "rdfs:Literal",
  "rdf:value": {
    "@value": "Hello!",
    "@language": "en"
  }
}

or is there some other structure that would make more sense?

Thanks!

gkellogg commented 3 years ago

Yes, the second pattern is what you'd want to do. Basically, JSON-LD is just the RDF data model, and literals don't have identifiers, but you can create nodes having an rdf:value, or other property that seems to solve your use case.

davidbarratt commented 3 years ago

Got it. Thanks that's really helpful!