Json-ld compactify @context ✓

Due to Mastodon sending large @context objects and me storing all objects with id differently, I end up storing stuff like:

{
  "@context": [
    "https://www.w3.org/ns/activitystreams",
    "https://w3id.org/security/v1",
    {
      "manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
      "toot": "http://joinmastodon.org/ns#",
      "featured": { "@id": "toot:featured", "@type": "@id" },
      "featuredTags": { "@id": "toot:featuredTags", "@type": "@id" },
      "alsoKnownAs": { "@id": "as:alsoKnownAs", "@type": "@id" },
      "movedTo": { "@id": "as:movedTo", "@type": "@id" },
      "schema": "http://schema.org#",
      "PropertyValue": "schema:PropertyValue",
      "value": "schema:value",
      "discoverable": "toot:discoverable",
      "Device": "toot:Device",
      "Ed25519Signature": "toot:Ed25519Signature",
      "Ed25519Key": "toot:Ed25519Key",
      "Curve25519Key": "toot:Curve25519Key",
      "EncryptedMessage": "toot:EncryptedMessage",
      "publicKeyBase64": "toot:publicKeyBase64",
      "deviceId": "toot:deviceId",
      "claim": { "@type": "@id", "@id": "toot:claim" },
      "fingerprintKey": { "@type": "@id", "@id": "toot:fingerprintKey" },
      "identityKey": { "@type": "@id", "@id": "toot:identityKey" },
      "devices": { "@type": "@id", "@id": "toot:devices" },
      "messageFranking": "toot:messageFranking",
      "messageType": "toot:messageType",
      "cipherText": "toot:cipherText",
      "suspended": "toot:suspended",
      "Hashtag": "as:Hashtag",
      "focalPoint": { "@container": "@list", "@id": "toot:focalPoint" }
    }
  ],
  "id": "https://somewhere.social/users/someone#main-key",
  "owner": "https://somewhere.social/users/someone",
  "publicKeyPem": "-----BEGIN PUBLIC KEY-----\nXXXXXXXXXXXXXXXXX\nXXXXXXXXXXXXXXXXX\nXXXXXXXXXXXXXXXXX\nXXXXXXXXXXXXXXXXX\n\n-----END PUBLIC KEY-----\n"
}

Does someone know of someone having worked on reducing the @context property? One can remove the entire stuff in braces without a loss of information.

Also this is new behavior starting with Mastodon 4.1.0.

I realized that I can answer my own question. It’s called json-ld expansion and the result would be:

[
  {
    "@id": "https://somewhere.social/users/someone#main-key",
    "https://w3id.org/security#owner": [
      {
        "@id": "https://somewhere.social/users/someone"
      }
    ],
    "https://w3id.org/security#publicKeyPem": [
      {
        "@value": "-----BEGIN PUBLIC KEY-----\nXXXXXXXXXXXXXXXXX\nXXXXXXXXXXXXXXXXX\nXXXXXXXXXXXXXXXXX\nXXXXXXXXXXXXXXXXX\n\n-----END PUBLIC KEY-----\n"
      }
    ]
  }
]

By then reapplying the context (json-ld compactification)

{
  "@context": [
    "https://www.w3.org/ns/activitystreams",
    "https://w3id.org/security/v1"
  ],
  "id": "https://somewhere.social/users/someone#main-key",
  "owner": "https://somewhere.social/users/someone",
  "publicKeyPem": "-----BEGIN PUBLIC KEY-----\nXXXXXXXXXXXXXXXXX\nXXXXXXXXXXXXXXXXX\nXXXXXXXXXXXXXXXXX\nXXXXXXXXXXXXXXXXX\n\n-----END PUBLIC KEY-----\n"
}

I hope I help someone write better code by posting this.

1 Like