Skip to content

Issue compacting using a context with a property value index #822

Open
@mgberg

Description

@mgberg

I'm running into (what seems like on the surface) a strange situation where documents with contexts containing an index can be parsed (e.g. expanded) correctly but cannot be round tripped back to the original form by compacting.

Consider the following example which comes directly from the JSON-LD specification here:

{
  "@context": {
    "@version": 1.1,
    "schema": "http://schema.org/",
    "name": "schema:name",
    "body": "schema:articleBody",
    "athletes": {
      "@id": "schema:athlete",
      "@container": "@index",
      "@index": "schema:jobTitle"
    }
  },
  "@id": "http://example.com/",
  "@type": "schema:SportsTeam",
  "name": "San Francisco Giants",
  "athletes": {
    "Catcher": {
      "@type": "schema:Person",
      "name": "Buster Posey"
    },
    "Starting Pitcher": {
      "@type": "schema:Person",
      "name": "Madison Bumgarner"
    }
  }
}

If I try to expand that JSON-LD, I get the following result:

[
  {
    "@id": "http://example.com/",
    "@type": [
      "http://schema.org/SportsTeam"
    ],
    "http://schema.org/athlete": [
      {
        "@type": [
          "http://schema.org/Person"
        ],
        "http://schema.org/name": [
          {
            "@value": "Buster Posey"
          }
        ],
        "http://schema.org/jobTitle": [
          {
            "@value": "Catcher"
          }
        ]
      },
      {
        "@type": [
          "http://schema.org/Person"
        ],
        "http://schema.org/name": [
          {
            "@value": "Madison Bumgarner"
          }
        ],
        "http://schema.org/jobTitle": [
          {
            "@value": "Starting Pitcher"
          }
        ]
      }
    ],
    "http://schema.org/name": [
      {
        "@value": "San Francisco Giants"
      }
    ]
  }
]

This is the correct expansion of the original document.

However, if I try to compact the expanded document using the original context, the JSON-LD playground returns the following error: jsonld.SyntaxError: Absolute IRI "schema:jobTitle" confused with prefix "schema".

Seems odd. Furthermore, if I use an absolute IRI as the error implies (i.e. "@index": "http://schema.org/jobTitle" instead of "@index": "schema:jobTitle"), the error isn't raised but the index isn't constructed correctly; the two athletes are sorted under "@none".

However, I can force it to compact the data to the original form using the following slightly modified context:

{
  "@context": {
    "@version": 1.1,
    "schema": "http://schema.org/",
    "name": "schema:name",
    "body": "schema:articleBody",
    "job_title": "schema:jobTitle",
    "athletes": {
      "@id": "schema:athlete",
      "@container": "@index",
      "@index": "job_title"
    }
  }
}

The problem with that, of course, is that the context is different from the original. However, the original context is technically still applicable since "job_title" alias cannot show up in the data (unless, of course, schema:jobTitle is used in a different way somewhere else in other data).

The JSON-LD playground was used to create the above examples, but its JSON-LD processor is not the only one that suffers from this issue; I originally happened upon it using a different JSON-LD processor.

I understand that I can avoid this issue by just using the modified context instead. However, I'm curious as to why it's even an issue in the first place. Why doesn't the compacting algorithm accept the index as defined in the original context (which can be used to expand the document correctly)? According to the error, it seems to expect an absolute IRI; if that's the case, why does the modified context work when "job_title" is also not an absolute IRI?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions