Conversion to JSON from YAML messing with key casing

I’m attempting to convert a port of my YAML frontmatter to JSON for my article schema, but the conversion messes with the case on my keys.

Method 1 (ideal)

Frontmatter

title: Example Post
schema:
    mainEntity:
        - "@type": Question
          name: What is an example question?
          acceptedAnswer:
              "@type": Answer
              text: Lorem, ipsum dolor sit amet consectetur adipisicing elit. Excepturi, quia iusto, illum ab numquam sunt cupiditate aspernatur necessitatibus quisquam nemo obcaecati cumque corrupti quidem blanditiis asperiores perspiciatis laboriosam nulla vel.

Template:

<script type="application/ld+json">
    {{ .Params.schema }}
</script>

Output:

<script type="application/ld+json">
    {"mainentity":[{"@type":"Question","acceptedAnswer":{"@type":"Answer","text":"A blockchain network is a revolutionary new type of network that's capable of being decentralized. With blockchain, it's possible for a website or app to “live” across many different servers—with each one being independently owned and operated so that no individual or company retains complete control over the network. Blockchain is what makes the new Web3 model possible."},"name":"What is blockchain technology?"}]}
</script>

Note that the same thing happens when using jsonify.

Any thoughts on how to avoid this?

Strange workaround (not ideal)

Strangely enough, if I make the schema key an array, and then access the first index, it works:

Frontmatter:

title: Example Post
schema:
-   mainEntity:
        - "@type": Question
          name: What is an example question?
          acceptedAnswer:
              "@type": Answer
              text: Lorem, ipsum dolor sit amet consectetur adipisicing elit. Excepturi, quia iusto, illum ab numquam sunt cupiditate aspernatur necessitatibus quisquam nemo obcaecati cumque corrupti quidem blanditiis asperiores perspiciatis laboriosam nulla vel.

Template:

<script type="application/ld+json">
    {{ index .Params.schema 0 }}
</script>

Output:

<script type="application/ld+json">
    {"mainEntity":[{"@type":"Question","acceptedAnswer":{"@type":"Answer","text":"Lorem, ipsum dolor sit amet consectetur adipisicing elit. Excepturi, quia iusto, illum ab numquam sunt cupiditate aspernatur necessitatibus quisquam nemo obcaecati cumque corrupti quidem blanditiis asperiores perspiciatis laboriosam nulla vel."},"name":"What is an example question?"}]}
</script>

Object keys in front matter are always converted to lowercase.
Array elements in front matter are left as-is (you have already discovered this).

You might consider placing the data in a page resource:

content/
└── post/
    └── test/
        ├── index.md
        └── schema.yaml

And access the data from your template with:

{{ with .Resources.Get "schema.yaml" }}
  {{ $data := . | transform.Unmarshal }}
  <pre>{{ jsonify (dict "indent" "  ") $data }}</pre>
{{ end }}
1 Like

That’s a much better workaround. Thanks for the suggestion!

Thoroughly out of curiosity, why is this the case?

A little history…

That’s helpful background. Thank you!