json-ld / json-ld.org

JSON for Linked Data's documentation and playground site
https://json-ld.org/
Other
863 stars 152 forks source link

Multi-node Graph Containers #845

Open zonotope opened 1 month ago

zonotope commented 1 month ago

Hi. I was surprised by the expansion behavior of JSON-LD including named-graph data indexing with multiple nodes per indexed graph.

With this extension of the blog-post example:

{
  "@context": {
    "@version": 1.1,
    "schema": "http://schema.org/",
    "name": "schema:name",
    "body": "schema:articleBody",
    "words": "schema:wordCount",
    "images": "schema:image",
    "caption": "schema:caption",
    "size": "schema:contentSize",
    "url": "schema:contentUrl",
    "imagesEn": "http://example.com/images/en/",
    "imagesDe": "http://example.com/images/de/",
    "post": {
      "@id": "schema:blogPost",
      "@container": [
        "@graph",
        "@index"
      ]
    }
  },
  "@id": "http://example.com/",
  "@type": "schema:Blog",
  "name": "World Financial News",
  "post": {
    "en": [
      {
        "@id": "http://example.com/posts/1/en",
        "body": "World commodities were up today with heavy trading of crude oil...",
        "images": [
          { "@id": "imagesEn:OilGraph" },
          { "@id": "imagesEn:OilProductionChart" }
        ],
        "words": 1539
      },
      {
        "@id": "imagesEn:oilGraph",
        "size": 2,
        "caption": "Crude oil prices for the past 10 years",
        "url": "http://example.com/images/en/oilGraph"
      },
      {
        "@id": "imagesEn:oilProductionChart",
        "size": 3,
        "caption": "Top oil producing nations per-capita",
        "url": "http://example.com/images/en/oilProductionChart"
      }
    ],
    "de": [
      {
        "@id": "http://example.com/posts/1/de",
        "body": "Die Werte an Warenbörsen stiegen im Sog eines starken Handels von Rohöl...",
        "images": [
          { "@id": "imagesDe:OilGraph" },
          { "@id": "imagesDe:OilProductionChart" }
        ],
        "words": 1204
      },
      {
        "@id": "imagesDe:oilGraph",
        "sizeMb": 2,
        "caption": "Rohölpreise der letzten 10 Jahre",
        "url": "http://example.com/images/en/oilGraph"
      },
      {
        "@id": "imagesDe:oilProductionChart",
        "sizeMb": 3,
        "caption": "Größte Ölförderländer pro Kopf",
        "url": "http://example.com/images/en/oilProductionChart"
      }
    ]
  }
}

I would expect the value for the "post" key to expand by grouping each node within the same index into an array value under a single "@graph" key, like this:

{
  //...
  "http://schema.org/blogPost": [
    {
      "@graph": [
        {
          "@id": "http://example.com/posts/1/de",
          "http://schema.org/articleBody": [
            {
              "@value": "Die Werte an Warenbörsen stiegen im Sog eines starken Handels von Rohöl..."
            }
          ],
          "http://schema.org/image": [
            {
              "@id": "http://example.com/images/de/OilGraph"
            },
            {
              "@id": "http://example.com/images/de/OilProductionChart"
            }
          ],
          "http://schema.org/wordCount": [
            {
              "@value": 1204
            }
          ]
        },
        {
          "@id": "http://example.com/images/de/oilGraph",
          "http://schema.org/caption": [
            {
              "@value": "Rohölpreise der letzten 10 Jahre"
            }
          ],
          "http://schema.org/contentUrl": [
            {
              "@value": "http://example.com/images/en/oilGraph"
            }
          ]
        },
        {
          "@id": "http://example.com/images/de/oilProductionChart",
          "http://schema.org/caption": [
            {
              "@value": "Größte Ölförderländer pro Kopf"
            }
          ],
          "http://schema.org/contentUrl": [
            {
              "@value": "http://example.com/images/en/oilProductionChart"
            }
          ]
        }
      ],
      "@index": "de"
    },
    {
      "@graph": [
        {
          "@id": "http://example.com/posts/1/en",
          "http://schema.org/articleBody": [
            {
              "@value": "World commodities were up today with heavy trading of crude oil..."
            }
          ],
          "http://schema.org/image": [
            {
              "@id": "http://example.com/images/en/OilGraph"
            },
            {
              "@id": "http://example.com/images/en/OilProductionChart"
            }
          ],
          "http://schema.org/wordCount": [
            {
              "@value": 1539
            }
          ]
        },
        {
          "@id": "http://example.com/images/en/oilGraph",
          "http://schema.org/caption": [
            {
              "@value": "Crude oil prices for the past 10 years"
            }
          ],
          "http://schema.org/contentSize": [
            {
              "@value": 2
            }
          ],
          "http://schema.org/contentUrl": [
            {
              "@value": "http://example.com/images/en/oilGraph"
            }
          ]
        },
        {
          "@id": "http://example.com/images/en/oilProductionChart",
          "http://schema.org/caption": [
            {
              "@value": "Top oil producing nations per-capita"
            }
          ],
          "http://schema.org/contentSize": [
            {
              "@value": 3
            }
          ],
          "http://schema.org/contentUrl": [
            {
              "@value": "http://example.com/images/en/oilProductionChart"
            }
          ]
        }
      ],
      "@index": "en"
    }
  ]
}

Instead, the JSON-LD playground does not group the nodes at all, but repeats each index key in the output for each node with that index. In each case within the expanded data, the value of the "@graph" key is an array with a single node instead of grouping all of the nodes with the same index into the same graph array, and each value of the "@index" key appears multiple times in the output.

JSON-LD data with named-graph indexing behaves the same way.

Is there a way to specify that all the nodes with the same index should be grouped into the same "@graph" array?

Thanks.

gkellogg commented 1 month ago

That's right out of example 119 in Named Graph Indexing. This makes the algorithm round-trippable.

If you want to coalesce values you can try using the flattening algorithm followed by framing back into the form you're looking for. You can also transform to RDF and back, which will have the same effect.