ThomasFrancart
ThomasFrancart

Reputation: 510

Order and nesting of entities in JSON-LD Framing

Given a JSON-LD containing entities of different types, can JSON-LD framing allow to control the order of these entities in the output ? and is it possible to nest (using @nest) some entities to group them ?

Given the input from the playground:

{
  "@context": "https://www.w3.org/ns/activitystreams",
  "@type": "Create",
  "actor": {
    "@type": "Person",
    "@id": "acct:[email protected]",
    "name": "Sally"
  },
  "object": {
    "@type": "Note",
    "content": "This is a simple note"
  },
  "published": "2015-01-25T12:34:56Z"
}

I would like to achieve this, with the garantee that the Persons will be after the Activity :

{
  "@context": "https://www.w3.org/ns/activitystreams",
  "@graph": [
    {
      "type": "Create",
      "actor": "acct:[email protected]",
      "object": {
        "type": "Note",
        "content": "This is a simple note"
      },
      "published": "2015-01-25T12:34:56Z"
    },
    {
      "id": "acct:[email protected]",
      "type": "Person",
      "name": "Sally"
    }
  ]
}

The following framing does what I want, but does not offer a garantee on order: (I tried with ["Person","Create"] and I still get the Activity before the Person)

{
  "@context": "https://www.w3.org/ns/activitystreams",
  "type": ["Person", "Create"],
  "actor": {
    "@embed" : "@never",
    "@omitDefault": true
  }
}

And, can I nest persons in a persons key ? like this:

{
  "@context": "https://www.w3.org/ns/activitystreams",
  "@graph": [
    {
      "type": "Create",
      "actor": "acct:[email protected]",
      "object": {
        "type": "Note",
        "content": "This is a simple note"
      },
      "published": "2015-01-25T12:34:56Z"
    },
    "actors": [
      {
        "id": "acct:[email protected]",
        "type": "Person",
        "name": "Sally"
      }
    ]
  ]
}

Upvotes: 0

Views: 398

Answers (1)

Gregg Kellogg
Gregg Kellogg

Reputation: 1906

Generally, ordering of values in JSON is limited to array values. In particular, keys in a Map structure do not have any inherent order in JSON. The JSON-LD algorithms will generally try to process keys in the order they are seen, but this doesn't include the use cases you are concerned with.

If the option for ordering keys is used, keys are ordered alphabetically.

In the case you cite, the "Note" is actually contained as the type of a key under "Create", so what you might want is to be sure that the object with type "Person" comes after the object with type "Create", but again, there's unfortunately no way to specify this, even though the values are ordered.

Your last example is actually invalid markup, as it is an array, but the second member of the array has a key/value pair.

Upvotes: 1

Related Questions