Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annotating nested structures/values in JSON-LD

Tags:

json-ld

Say I have a JSON object with some properties in a nested object.

{
    "title": "My Blog Post",
    "meta": {
        "publishedAt": "2016-08-01T00:00:00Z"
    }
}

Is there an easy way I can just add a @context to my top-level object to reach these properties (i.e. just "pass through" the meta object)? Something along these lines:

{
    "@context": {
        "title": "schema:name",
        "meta.publishedAt": {
            "@type": "xsd:date",
            "@id": "schema:datePublished"
        }
    },

    "@id": "/my-article",
    "title": "My Blog Post",

    "meta": {
        "publishedAt": "2016-08-01T00:00:00Z"
    }
}

I would like to avoid having to add (duplicate) @id to the nested object, which is how I would otherwise have solved it:

{
    "@context": {
        "title": "schema:name",
        "meta": { "@id": "_:meta", "@container": "@set" },
        "publishedAt": {
            "@type": "xsd:date",
            "@id": "schema:datePublished"
        }
    },

    "@id": "/my-article",
    "title": "My Blog Post",

    "meta": {
        "@id": "/my-article",
        "publishedAt": "2016-08-01T00:00:00Z"
    }
}

This solution works, but requires duplication, and comes from ethanresnick's comments on Github about annotating JSON API. He noted in another issue that @context is not "quite expressive enough to annotate the JSON API structure". I was hoping to prove him wrong at least with regards to this issue.

like image 410
vicvicvic Avatar asked Oct 26 '25 10:10

vicvicvic


1 Answers

I just discovered that the latest JSON-LD spec includes a new section on nested properties. Defining your context like this should result in the desired output:

{
    "@context": {
        "title": "schema:name",
        "meta": "@nest",
        "publishedAt": {
            "@type": "xsd:date",
            "@id": "schema:datePublished",
            "@nest": "meta"
        }
    },
    ...
}
like image 76
cbst Avatar answered Oct 29 '25 09:10

cbst



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!