digitalbazaar/pyld

Problem compacting data

Closed this issue · 4 comments

Hello,

When I try the following code:

 from pyld import jsonld
 doc = {"@type": "Movie",
       "actor": [{"name": "Myers Clark",
             "sameAs": "http://akas.imdb.com/name/nm2072773/"},
            {"name": "Kimberly Ridgeway",
             "sameAs": "http://akas.imdb.com/name/nm2064470/"}]}
 print jsonld.compact(doc, "http://schema.org/")

I get this as a respose:

{'@context': 'http://schema.org/', '@type': 'Movie'}

However, when I try this equivalent code in http://json-ld.org/playground/,

 {"@context": "http://schema.org",
   "@type": "Movie",
  "actor": [{"name": "Myers Clark",
             "sameAs": "http://akas.imdb.com/name/nm2072773/"},
            {"name": "Kimberly Ridgeway",
             "sameAs": "http://akas.imdb.com/name/nm2064470/"}]}

I get the expected json-ld in the compacted view:

 {
   "@type": "http://schema.org/Movie",
   "http://schema.org/actor": [
     {
       "http://schema.org/name": "Myers Clark",
       "http://schema.org/sameAs": {
         "@id": "http://akas.imdb.com/name/nm2072773/"
       }
     },
     {
       "http://schema.org/name": "Kimberly Ridgeway",
      "http://schema.org/sameAs": {
         "@id": "http://akas.imdb.com/name/nm2064470/"
       }
     }
   ]
 }

Any idea why jsonld.compact is ignoring the actors in this example?

Thanks :)

You're giving the playground different input. The input (doc) you're passing to pyld doesn't have a @context, so terms like name and actor get dropped during processing, as they are undefined. Make sure you're including the context, which looks like it should be http://schema.org, just like in your playground example:

doc = {
    "@context": "http://schema.org",
    "@type": "Movie",
    "actor": [{
        "name": "Myers Clark",
        "sameAs": "http://akas.imdb.com/name/nm2072773/"
    }, {
        "name": "Kimberly Ridgeway",
        "sameAs": "http://akas.imdb.com/name/nm2064470/"
    }]
}

There may also be some misunderstanding over what compact does; as your input is already using the schema.org context and you're trying to compact it to that same context. The primary reason to use compact is to change an input document from one context to another (different one).

If your document is already using the schema.org context, there's generally no need to compact it to that same context. That isn't always the case (compaction can make some other minor changes to the output, as it may more effectively apply the context, eg: if you had some full URLs as properties it would compact them using the context), but it looks like it's unnecessary here.

Looking at the output you said you want, it sounds like you actually want to do expansion, not compaction. Expansion will remove context from an input document. That output you pasted from the playground looks like it came from the expansion tab, not the compaction tab. In that case, make sure you've got @context on your input doc (like above) and call jsonld.expand instead. That should give you what you want.

Thanks a lot for the very detailed response. Now that I've slept over it, and with your help, I can finally understand why the program was outputting something different then what I was expecting.

I've managed to make it work now.

For now, my interest was really only to check if the json-ld document is valid and if all properties I specify in the document are being correctly identified according to the context. An "easy" way to do this is to compact the file against its own context, and check if the resulting document is the same as the original one. Don't you agree?

Thanks once more,

Thanks a lot for the very detailed response.

Sure!

An "easy" way to do this is to compact the file against its own context, and check if the resulting document is the same as the original one. Don't you agree?

Sure, that's probably an ok way to do a quick de facto validation.

👍 😄