# Convert between toml, yaml and json in shortcode

**URL:** <https://discourse.gohugo.io/t/convert-between-toml-yaml-and-json-in-shortcode/47134>\
**Category:** support\
**Created:** [November 17, 2023, 11:18am UTC](https://discourse.gohugo.io/t/convert-between-toml-yaml-and-json-in-shortcode/47134 "2023-11-17T11:18:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![McShelby](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mcshelby/32/15112_2.png) [@McShelby](https://discourse.gohugo.io/u/McShelby)\
**Post date:** [November 17, 2023, 11:18am UTC](https://discourse.gohugo.io/t/convert-between-toml-yaml-and-json-in-shortcode/47134/1 "2023-11-17T11:18:25Z")

</div>

I have some code examples in toml that are displayed in code blocks. I’ve seen in the Hugo docs that these examples are provided in all three forms, toml, yaml and json. Is there a way to convert between these formats inside of a shortcode programmatically to avoid the duplication of my examples? It would also ~~need~~ be nice to convert the comments inside of the toml files.

---

<div class="post-metadata">

**Author:** ![razon](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/razon/32/14882_2.png) [@razon](https://discourse.gohugo.io/u/razon)\
**Post date:** [November 17, 2023, 11:52am UTC](https://discourse.gohugo.io/t/convert-between-toml-yaml-and-json-in-shortcode/47134/2 "2023-11-17T11:52:45Z")

</div>

You can 1) `transform.Unmarshal` and then 2) `transform.Remarshal` it in other format.

```go
{{ $code := printf "%s\n%s" "foo: bar # my comment" "hello: world" }}
{{ $lang := "toml" }}
{{ $toml := $code | transform.Unmarshal | transform.Remarshal $lang }}
{{ $toml | warnf "\n%s" }}

```

But I don’t think it’s able to keep the comments.

> Comments are not valid in JSON format.

- [https://github.com/gohugoio/hugo/blob/27620daa20bfd951971f98c8177488fc46d7637c/tpl/transform/remarshal.go#L20](https://github.com/gohugoio/hugo/blob/27620daa20bfd951971f98c8177488fc46d7637c/tpl/transform/remarshal.go#L20)
- [https://github.com/gohugoio/hugo/blob/27620daa20bfd951971f98c8177488fc46d7637c/tpl/transform/unmarshal.go#L38](https://github.com/gohugoio/hugo/blob/27620daa20bfd951971f98c8177488fc46d7637c/tpl/transform/unmarshal.go#L38)

---

<div class="post-metadata">

**Author:** ![McShelby](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mcshelby/32/15112_2.png) [@McShelby](https://discourse.gohugo.io/u/McShelby)\
**Post date:** [November 17, 2023, 1:11pm UTC](https://discourse.gohugo.io/t/convert-between-toml-yaml-and-json-in-shortcode/47134/3 "2023-11-17T13:11:30Z")

</div>

Looks pretty magic as the transform.Unmarshal does not take a lang specifier. Will try this out later.

Well comments would be really nice to have, but you’re right, comments are not allowed in plain JSON. I modifiied my request above.

---

<div class="post-metadata">

**Author:** ![razon](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/razon/32/14882_2.png) [@razon](https://discourse.gohugo.io/u/razon)\
**Post date:** [November 17, 2023, 8:50pm UTC](https://discourse.gohugo.io/t/convert-between-toml-yaml-and-json-in-shortcode/47134/4 "2023-11-17T20:50:19Z")

</div>

Found the [related issue](https://github.com/gohugoio/hugo/issues/4520) on GitHub.
