w3c / w3c/json-ld-syntax

Type Coercion / Node Conversion: @coerce keyword or similar

Open
#335 5 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

defer-future-version
Dominant language
HTML
Stars
139
Forks
23
PR merge metrics
No merged PRs in 30d

Description

I have an idea for a future version of JSON-LD.

As a web developer, I would like to receive a JSON document from i.e. a simple API, add a @context to it, and perform JSON-LD operations. This light approach has been a big selling point to me for using JSON-LD.

A common response from such an API may include an "id" property as a number (as it is indexed in the source database as an integer), so after applying the @context the document may look like this:

{
  "@context": {
    "@base": "http://starwars.com/",
    "@vocab": "http://starwars.com/",
    "id": "@id",
    "type": "@type"
  },
  "id": 123,
  "type": "Human",
  "firstName": "Luke",
  "lastName": "Skywalker"
}

Currently, the expansion step will generate an error:

Invalid JSON-LD syntax; "@id" value must a string.

My intention in declaring the @base was to form the IRI "http://starwars.com/123". I don't think this kind of type coercion is currently possible with JSON-LD.

The alternative would be a blank node with a custom ID property, which is less useful in certain situations.

I propose a solution that coerces the XSD type, so that when flattened, it becomes:

[
  {
    "@id": "http://starwars.com/123",
    "@type": [
      "http://starwars.com/Human"
    ],
    "http://starwars.com/firstName": [
      {
        "@value": "Luke"
      }
    ],
    "http://starwars.com/lastName": [
      {
        "@value": "Skywalker"
      }
    ]
  }
]

As a starting point, I suggest adding a keyword called @coerce.

I originally opened this as an issue in Framing and got some helpful feedback from @gkellogg:

https://github.com/w3c/json-ld-framing/issues/98

This would probably be in compaction/expansion rather than framing. It is somewhat similar to the useNativeTypes option for fromRdf.

It’s a common idiom in JSON, so it’s worthy of future consideration.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the JSON-LD 1.1 syntax around expansion and compaction, then read the referenced json-ld-framing issue for prior feedback. The proposed @coerce behavior and its placement need agreement; done means the specification defines the behavior and includes corresponding tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
json
Domain
documentation
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.