Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no term-to-IRI expansion #130

Closed
mfrigerio17 opened this issue Jun 25, 2020 · 4 comments
Closed

no term-to-IRI expansion #130

mfrigerio17 opened this issue Jun 25, 2020 · 4 comments

Comments

@mfrigerio17
Copy link

Hi, I am new to JSON-LD so please excuse me if the question does not make much sense.
Consider the following:

import json
from pyld import jsonld

c = jsonld.compact( {'key':'value'}, {'key':'http://whatever.org/key'} )
print(c)  # {'@context': {'key': 'http://whatever.org/key'}} ## 'key':'value' is gone
c = jsonld.compact( {'http://whatever.org/key':'value'}, {'key':'http://whatever.org/key'} )
print(c)  #{'@context': {'key': 'http://whatever.org/key'}, 'key': 'value'}

In the first snippet, key is not matched with the term in the given context.

Why do I have to write the full IRI ('http://whatever.org/key') explicitly in the document ??
That kind of voids one of the advantages of having a context, doesn't it?

I think this behaviour is not consistent with the JSON-LD playground, where the first example would result in a compacted document which includes the term key:value. See here
The specs say "Term-to-IRI expansion occurs if the key matches a term defined within the active context", but that does not seem to be happening in PyLD (see example 11 of the specs)

Thank you

@davidlehn
Copy link
Member

The compact() call is compacting JSON-LD data with a new context. Effectively it will call expand() on the input data and then run the compaction algorithm with the new context. When {'key': 'value'} is expanded, it doesn't know what key is so the output is empty. It then compacts empty data which results in empty output.

e = jsonld.expand( {'key':'value'} )
print(e) # []

The choices here are to make the input valid by either adding the context or use the expandContext option. These will allow the input to be understood when compacting.

# add context to input
ctx = {'key':'http://whatever.org/key'}
c = jsonld.compact( {'@context': ctx, 'key':'value'}, ctx )
print(c)  # {'@context': {'key': 'http://whatever.org/key'}, 'key': 'value'}

# use expandContext option
ctx = {'key':'http://whatever.org/key'}
c = jsonld.compact( {'key':'value'}, ctx, {'expandContext': ctx} )
print(c)  # {'@context': {'key': 'http://whatever.org/key'}, 'key': 'value'}

The use case for compacting with the same context as the input is usually to convert full URLs to shorter terms.
But when using different contexts, you can change the names as needed.

# use expandContext option
ctx1 = {'key':'http://whatever.org/key'}
ctx2 = {'altKeyName':'http://whatever.org/key'}
c = jsonld.compact( {'@context': ctx1, 'key':'value'}, ctx2 )
print(c)  # {'@context': {'altKeyName': 'http://whatever.org/key'}, 'altKeyName': 'value'}

@mfrigerio17
Copy link
Author

Hi @davidlehn , thanks for your reply.
I understand what you are saying, and your examples.

I think I misunderstood the purpose of compacting. My target is to process the json-ld document to expand all the keys, remove the context, and be left with unambiguous (because the keys are IRIs) json (not LD). That seems to be the purpose of expansion, although expansion introduces all those unnecessary arrays []...

Still, the compaction in the playground does exactly what I want (hence my confusion), by turning

{
  "@context": {
    "key" : "http://whatever.org/key"
  },
  "key" : "value"
}

into

{
  "http://whatever.org/key": "value"
}

which is the same as expansion, but without unnecessary arrays. This is not achievable with the PyLD API, I suppose.
Is it a non-standard thing of the playground, perhaps?

PS.: for the convenience of the reader, here is the expanded doc:

[
  {
    "http://whatever.org/key": [
      {
        "@value": "value"
      }
    ]
  }
]

@davidlehn
Copy link
Member

Perhaps you want to compact with an empty context {}? You'll still need to ensure the input data is understood by having a context in it or using the expandContext option.

Depending on your data and options used this might cause some undesired changes. Specifically related to arrays being used or not used (but there are other cases). There is a compactArrays option, but that effects everything and doesn't have a "preserve" sort of mode. I'm not sure if there's an easy way to do that.

@mfrigerio17
Copy link
Author

Thanks, using the empty context does what I want.
Now I realize that that is also the case in the playground. Sorry about it, I overlooked the {} in the right pane.

I'll keep an eye for undesired side effects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants