# Help with formatting array for json

**URL:** <https://discourse.gohugo.io/t/help-with-formatting-array-for-json/16368>\
**Category:** support\
**Created:** [January 13, 2019, 9:46pm UTC](https://discourse.gohugo.io/t/help-with-formatting-array-for-json/16368 "2019-01-13T21:46:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pancake](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@pancake](https://discourse.gohugo.io/u/pancake)\
**Post date:** [January 13, 2019, 9:46pm UTC](https://discourse.gohugo.io/t/help-with-formatting-array-for-json/16368/1 "2019-01-13T21:46:39Z")

</div>

With the following front matter:

```
+++
author = ["author1", "author2"]
title = "Book Title"
+++

```

I would like to achieve this structure:

```
"title" : "Book Title",
    "author": {
      "name": "Author1"
    },
    "author": {
      "name": "Author2"
    }

```

I’m having difficulties with ranging over the `author` to create two separate author sections.

My code in `list.html`:

```
  {{range $i, $e := where .Pages "Section" "books"}}
    {{- if $i }}, {{end}}
      "title" : "{{.Title}}",
      {{range $i, $e := .Params.author}}
        {{- if $e }}, {{end}}
          "author": {
           "name":"{{$e}}"
          }
      {{end}}
  {{end}}

```

This gives:

```
"title" : "Book Title",
    "author": {
      "name": "Author1, Author2"
    }
```

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [January 13, 2019, 10:20pm UTC](https://discourse.gohugo.io/t/help-with-formatting-array-for-json/16368/2 "2019-01-13T22:20:33Z")

</div>

I suspect that you could do this simpler by something ala (typed and untested):

```auto
{{ $book := dict "title" .Title "author" .Params.author }}
{{ $bookJSON := $book | jsonify }}

```

---

<div class="post-metadata">

**Author:** ![pancake](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@pancake](https://discourse.gohugo.io/u/pancake)\
**Post date:** [January 13, 2019, 10:29pm UTC](https://discourse.gohugo.io/t/help-with-formatting-array-for-json/16368/3 "2019-01-13T22:29:25Z")

</div>

Thanks, that code snippet is helpful. But, it doesn’t solve the problem.

It gives the following output:

```
{"author":["DHH, Jason Fried"],"title":"ReWork"}
{"author":["Yuval Noah Harari"],"title":"Sapiens"}

```

For ReWork, I’m looking for the output to be as shown in the initial post, the authors to be separated. I’ll continue working on this. Appreciate the help.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [January 13, 2019, 11:31pm UTC](https://discourse.gohugo.io/t/help-with-formatting-array-for-json/16368/4 "2019-01-13T23:31:54Z")

</div>

What is your “full code” with my suggested way? I say this because I think you have a “range too many”.

---

<div class="post-metadata">

**Author:** ![pancake](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@pancake](https://discourse.gohugo.io/u/pancake)\
**Post date:** [January 13, 2019, 11:40pm UTC](https://discourse.gohugo.io/t/help-with-formatting-array-for-json/16368/5 "2019-01-13T23:40:00Z")

</div>

Complete code in template `list.html`

```
{{range where (where .Pages "Section" "books") "Layout" "ne" "keyword"}}
{{$book := dict "title" .Title "author" .Params.author}}
{{$bookJSON := $book | jsonify}}
{{$bookJSON}}
{{end}}
```

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [January 13, 2019, 11:46pm UTC](https://discourse.gohugo.io/t/help-with-formatting-array-for-json/16368/6 "2019-01-13T23:46:18Z")

</div>

Yes, OK, sorry, I misread your original question. I will have to get back to you on this. I’m writing this in bed, dead tired …

---

<div class="post-metadata">

**Author:** ![pancake](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@pancake](https://discourse.gohugo.io/u/pancake)\
**Post date:** [January 13, 2019, 11:47pm UTC](https://discourse.gohugo.io/t/help-with-formatting-array-for-json/16368/7 "2019-01-13T23:47:35Z")

</div>

No worries. Again appreciate the help. I’m beat too so I’m going to revisit this tomorrow. Thanks a bunch.

---

<div class="post-metadata">

**Author:** ![pancake](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@pancake](https://discourse.gohugo.io/u/pancake)\
**Post date:** [January 14, 2019, 4:37pm UTC](https://discourse.gohugo.io/t/help-with-formatting-array-for-json/16368/8 "2019-01-14T16:37:03Z")

</div>

This is the solution I’ve come up with. Let me know if there is a better way.

```
  {{range $i, $e := where .Pages "Section" "books"}}
        {{- if $i }}, {{end}}
          "title" : "{{.Title}}",
          {{range $i, $e := .Params.author}}
            {{- if $i }}, {{end}}
              "author": {
               "name":"{{$e}}"
              }
          {{end}}
      {{end}}

```

This is actually identical to the code I provided in my original post. The mistake I did previously was use `{{if $e}}` instead of `{{if $i}}`.
