digitalbazaar / jsonld.js

A JSON-LD Processor and API implementation in JavaScript
https://json-ld.org/
Other
1.66k stars 195 forks source link

Question: difference in behaviour to playground #329

Closed marcolarosa closed 4 years ago

marcolarosa commented 5 years ago

If I run the following node script (using jsonld v1.8):

const jsonld = require("jsonld");

const data = {
    "@id": "./",
    "http://pcdm.org/models#hasMember": [
        {
            "@id": "paradisec.org.au/AC1/001"
        }
    ]
};

(async () => {
    let root = await jsonld.compact(data, {
        "@context": "https://schema.org"
    });
    console.log(JSON.stringify(root, null, 2));
})();

I get:

{
  "@context": "https://schema.org",
  "id": "/",
  "http://pcdm.org/models#hasMember": {
    "id": "/paradisec.org.au/AC1/001"
  }
}

In the playground however, the same data compacted gives:

{
  "@context": "https://schema.org",
  "id": "./",
  "http://pcdm.org/models#hasMember": {
    "id": "paradisec.org.au/AC1/001"
  }
}

Notice that the value for the "hasMember" id in the script output has a / prepended to it (/paradisec... vs paradisec...) that doesn't get added in the playground.

Why is that?

davidlehn commented 5 years ago

The playground is running 1.8.0. Difference is likely the base URI you are processing with and that those ids are relative. That paradisec.org id should probably start with http or https? Otherwise it would be relative to the playground URL itself.

marcolarosa commented 5 years ago

@davidlehn thankyou!

How can I get my script to behave the same as the playground? I don't see base defined anywhere in the playground and I'm not defining a base anywhere as far as I can tell but I do want relative URLs.

davidlehn commented 5 years ago

The playground uses it's own URL as the base, IIRC. We had to guess what the least confusing default would be. It should be an adjustable UI option but no one has added that feature yet. The jsonld.js compact API has a base option you can set to whatever you'd like.

marcolarosa commented 5 years ago

@davidlehn Ahh the lack of a configurable base in the playground explains one part of the mystery! I'm just looking at the jsonld docs and I don't see the option for configuring the base (though that may be my lack of familiarity with the tool).

Are you able to provide a small example of how I would use compact?

Thanks heaps!

davidlehn commented 5 years ago

Like this but with an options param: https://github.com/digitalbazaar/jsonld.js/#compact

const compacted = await jsonld.compact(doc, context, {base: ...});

The API options docs are just in the code at the moment: https://github.com/digitalbazaar/jsonld.js/blob/master/lib/jsonld.js.

marcolarosa commented 5 years ago

@davidlehn

ok - thanks for the info.

I've been diving around in the jsonld.js and compact.js code and it seems like the prefixing is happening long before the compaction code. In fact, it looks like the required option is skipExpansion: true, viz:

(async () => {
    let root = await jsonld.compact(data, {
            "@context": "https://schema.org",
        },
        {
            "skipExpansion": true,
        }

    );
    console.log(JSON.stringify(root, null, 2));
})();

Results in (what I'm expecting):

{
  "@context": "https://schema.org",
  "id": "./",
  "http://pcdm.org/models#hasMember": {
    "id": "paradisec.org.au/AC1/001"
  }
}

I also tried with:

        {
            "base": null,
            "compactArrays": false,
            "compactToRelative": true,
            "skipExpansion": true,
        }

But it was skipExpansion that did the trick.

Does that seem right to you?

dlongley commented 4 years ago

Closing as stale/this should be filed under json-ld.org as an issue or feature for the playground.