# How to unmarshal JSON containing shortcode

**URL:** <https://discourse.gohugo.io/t/how-to-unmarshal-json-containing-shortcode/48154>\
**Category:** support\
**Created:** [February 6, 2024, 10:17pm UTC](https://discourse.gohugo.io/t/how-to-unmarshal-json-containing-shortcode/48154 "2024-02-06T22:17:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefanv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/stefanv/32/16437_2.png) [@stefanv](https://discourse.gohugo.io/u/stefanv)\
**Post date:** [February 6, 2024, 10:17pm UTC](https://discourse.gohugo.io/t/how-to-unmarshal-json-containing-shortcode/48154/1 "2024-02-06T22:17:31Z")

</div>

We have the following:

```auto
{{< card >}}
title: Heading and body
body:
  Content of the third card.
  {{< badge primary >}}Sample badge{{< /badge >}}
{{< /card >}}

```

We unmarshal this in the `card` shortcode, using `transform.Unmarshal`. That fails, stating:

> error calling Unmarshal: “\_stream.yaml:17:1”: failed to unmarshal YAML: yaml: line 17: could not find expected ‘:’

Are we incorrect in expecting that JSON string to come back unprocessed? Even if the shortcode is evaluated first (which it shouldn’t be), the result is valid YAML. So any idea what might be going wrong?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 6, 2024, 10:45pm UTC](https://discourse.gohugo.io/t/how-to-unmarshal-json-containing-shortcode/48154/2 "2024-02-06T22:45:58Z")

</div>

What is the badge shortcode?

Something like this will fail:

```plaintext
<div class="{{ .Get 0 }}">
  {{ .Inner }}
</div>

```

…because the closing `div` starts a column 0, not column 2 (the joys of YAML indentation). You would need to do something like this instead:

```plaintext
<div class="{{ .Get 0 }}">
  {{ .Inner }}
  </div>

```

You don’t need to indent the first line because it is already indented in the parent.

As soon as lose the indentation, you terminate the block, so the line is a key, and you don’t have a `: value` after it, hence the error.

[https://yaml-multiline.info/](https://yaml-multiline.info/)

---

<div class="post-metadata">

**Author:** ![stefanv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/stefanv/32/16437_2.png) [@stefanv](https://discourse.gohugo.io/u/stefanv)\
**Post date:** [February 7, 2024, 12:16am UTC](https://discourse.gohugo.io/t/how-to-unmarshal-json-containing-shortcode/48154/3 "2024-02-07T00:16:57Z")

</div>

Oh, wow, that is unexpected. The shortcode indeed generated something like `<span>...</span>`, but probably with a few leading and trailing newlines.

To work around that issue, the best I have for now is an `inline` shortcode that indents content acceptably:

```auto
{{ $body := trim .Inner "\n" }}
{{- $m := findRE "^[]*" $body -}}
{{- $indent := len (index $m 0) -}}
{{- $prefix := strings.Repeat $indent " " -}}
{{- range (split $body "\n") -}}
{{ $prefix }}{{ . | safeHTML }}
{{ end }}

```

To be used as:

```auto
{{< card >}}
title: Heading and body
body:
  Content of the third card.

  {{< inline >}}
    {{< badge primary >}}Sample badge{{< /badge >}}
  {{< /inline >}}
{{< /card >}}

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 7, 2024, 12:28am UTC](https://discourse.gohugo.io/t/how-to-unmarshal-json-containing-shortcode/48154/4 "2024-02-07T00:28:16Z")

</div>

That looks really fragile to me. I’m not sure what you’re doing, and I really don’t want to know, but you might step back for a minute or two… maybe there’s a more solid way of doing what you want.

---

<div class="post-metadata">

**Author:** ![stefanv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/stefanv/32/16437_2.png) [@stefanv](https://discourse.gohugo.io/u/stefanv)\
**Post date:** [February 7, 2024, 5:43pm UTC](https://discourse.gohugo.io/t/how-to-unmarshal-json-containing-shortcode/48154/5 "2024-02-07T17:43:08Z")

</div>

More robust may be to switch to an indent-less format like TOML. (Tested and that works without jumping through any hoops.)
