DEV Community

Avi Aryan
Avi Aryan

Posted on

Rendering JSON-LD data in NextJS and ReactJS

Rendering JSON-LD data in NextJS and ReactJS is quite straight-forward. Here are the steps involved.

  1. Organize data in a plain JavaScript Object.
  2. Serialize it using JSON.stringify.
  3. Include the data in the script tag using dangerouslySetInnerHTML.
  4. Optional but recommended - Attach a key.

The first step is creating the JSON Object. I used this in GoRemote which is a job-listing site. Here is the function which creates a job schema given the job object. It returns a simple JavaScript object.

function makeJobSchema(job) {
    const desc = stripHTML(job.description)
    return {
        // schema truncated for brevity
        '@context': '',
        '@type': 'JobPosting',
        datePosted: job.postedAt,
        description: desc,
        title: job.title,
        workHours: 'Flexible',
        validThrough: addDaysToDate(job.postedAt, 60),
        hiringOrganization: {
            '@type': 'Organization',
            sameAs: || null,

The next step is to serialize this object using JSON.stringify. Once serialized, we insert it in a script tag using dangerouslySetInnerHTML. Here is how the code looks like.

export default function JobSchema({ job }) {
    return (
            dangerouslySetInnerHTML={{ __html: JSON.stringify(makeJobSchema(job)) }}

Now, you might question, why do we use dangerouslySetInnerHTML? Why not insert the serialized object as a string? Because if we do that, React treats it as HTML and so HTML-escapes the text. The output then looks like this.

<script type="application/ld+json">

This won’t work with bots that will crawl the page for this metadata. In other words, we don’t want to HTML-escape the text within these script tags. So, that’s why we use dangerouslySetInnerHTML.

Another question would be why do you need to add a key? It’s not needed but it’s a good-to-have to avoid the situation where you have multiple JSON-LD’s for a single entity. A key makes sure that only one such instance is included in the rendered page.

Well, there you have it.

Once you have the page running, you can use ngrok to expose your localhost online and Google’s structured data testing tool to make sure everything works as expected.

This article was posted on Avi Aryan's Blog.

Discussion (4)

alioshr profile image
Aliosh Romano • Edited

Hello there!

Could you please explain what is this job.description obj instance?

Are you referring to this?

Like, for instance, on, it would be the description instance, with the description: " A description of the item."

fvaldes33 profile image
Franco Valdes

Big help here! Thanks for the post.

acventor profile image

Many thanks! Your article helped me a lot