Ad

JSON-LD Compaction And Compact IRIs As Values

- 1 answer

I'm confused slightly by JSON-LD compaction and whether it can be used to compact the IRIs of values.

I have the following JSON-LD object

{
    "@context": {
        "@base": "file:///", 
        "x": "https://example.org/pub/x#", 
        "x-attribute": "https://example.org/pub/x-attribute#",
        "x:purpose": { 
            "@type": "@id"
        }
    },
    "https://example.org/pub/x#purpose": "https://example.org/pub/x-attribute#on"
}

and the following new context

{
    "x": "https://example.org/pub/x#",
    "x-attribute": "https://example.org/pub/x-attribute#"
}

I'm expecting ... and want ... to get

{
  "@context": {
    "x": "https://example.org/pub/x#",
    "x-attribute": "https://example.org/pub/x-attribute#"
  },
  "x:purpose": "x-attribute:on"
}

but what I end up getting is

{
  "@context": {
    "x": "https://example.org/pub/x#",
    "x-attribute": "https://example.org/pub/x-attribute#"
  },
  "x:purpose": "https://example.org/pub/x-attribute#on"
}

You can plug this into the JSON-LD Playground if you'd like to try this.

How I can accomplish what I'm trying to do? I.e. basically use Compact IRIs in value positions.

Ad

Answer

First a quick note: you aren't using the term you've defined in the context in the input object. Since you're using the full URI, the @type definition is not being applied. Instead you should use the term (x:purpose):

{
    "@context": {
        "@base": "file:///", 
        "x": "https://example.org/pub/x#", 
        "x-attribute": "https://example.org/pub/x-attribute#",
        "x:purpose": { 
            "@type": "@id"
        }
    },
    "x:purpose": "https://example.org/pub/x-attribute#on"
}

If you don't use the term in the data, you'll need to specify that the value is an @id like so:

{
    "@context": {
        "@base": "file:///", 
        "x": "https://example.org/pub/x#", 
        "x-attribute": "https://example.org/pub/x-attribute#",
        "x:purpose": { 
            "@type": "@id"
        }
    },
    "https://example.org/pub/x#purpose": {
        "@id": "https://example.org/pub/x-attribute#on"
    }
}

Now, to get the effect you want where the value is compacted to a CURIE, you must indicate that the value is actually part of your vocabulary (an "enum" if you will). You do this by changing the new context to:

{
    "x": "https://example.org/pub/x#",
    "x-attribute": "https://example.org/pub/x-attribute#",
    "x:purpose": {
        "@type": "@vocab"
    }
}

That should give you the result you want.

Ad
source: stackoverflow.com
Ad