# Add new key value to a map

**URL:** <https://discourse.gohugo.io/t/add-new-key-value-to-a-map/19607>\
**Category:** support\
**Created:** [July 12, 2019, 12:52am UTC](https://discourse.gohugo.io/t/add-new-key-value-to-a-map/19607 "2019-07-12T00:52:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![khoan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/khoan/32/7856_2.png) [@khoan](https://discourse.gohugo.io/u/khoan)\
**Post date:** [July 12, 2019, 12:52am UTC](https://discourse.gohugo.io/t/add-new-key-value-to-a-map/19607/1 "2019-07-12T00:52:27Z")

</div>

```
$ hugo version
Hugo Static Site Generator v0.54.0 darwin/amd64 BuildDate: unknown
$ cat layouts/t/code.html
  ...
  {{- $json := getJSON $path -}}
  {{- if eq $action "edit" -}}
    {{- $json.nestedMap["action"] = "update" -}}
  {{- end -}}
  ...
  <script type="module">
    import App from "/code.js";
    new App({{ $json.nestedMap | jsonify }});
  </script>

```

> $json.nestedMap is map[string]interface {}

but got error parse failed bad character U+005B ‘[’

Thanks for any tips.

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [July 12, 2019, 2:12am UTC](https://discourse.gohugo.io/t/add-new-key-value-to-a-map/19607/2 "2019-07-12T02:12:16Z")

</div>

**Edit:** on closer look, we need more info. Your title says you want to add a new entry to a map. But your example says you want to set the value of a map entry. Would you clarify this

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [July 12, 2019, 1:10pm UTC](https://discourse.gohugo.io/t/add-new-key-value-to-a-map/19607/3 "2019-07-12T13:10:50Z")

</div>

You need to post at least the existing map and the new entry for us to know what the issue might be. The error could be more verbose too, it might give you at least a line where the error comes up.

`$json.nestedMap["action"]` looks weird to me. how about `$json.nestedMap.action`?

---

<div class="post-metadata">

**Author:** ![khoan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/khoan/32/7856_2.png) [@khoan](https://discourse.gohugo.io/u/khoan)\
**Post date:** [July 12, 2019, 1:37pm UTC](https://discourse.gohugo.io/t/add-new-key-value-to-a-map/19607/4 "2019-07-12T13:37:32Z")

</div>

Thanks. I’d like to add value to a key that is not found in the map.

$path is a /data/meta.json file.

```
$ cat data/meta.json
{
  "nestedMap": {
    "title": "Meta is meta"
  }
}
```

---

<div class="post-metadata">

**Author:** ![khoan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/khoan/32/7856_2.png) [@khoan](https://discourse.gohugo.io/u/khoan)\
**Post date:** [July 12, 2019, 1:46pm UTC](https://discourse.gohugo.io/t/add-new-key-value-to-a-map/19607/5 "2019-07-12T13:46:42Z")

</div>

Thanks.

```
{{ printf "%#v" $json.nestedMap }}

```

shows map[string]interface {}{"title":"Meta is meta"}

```
$ hugo server --debug
...
**ERROR** 2019/07/12 23:38:30 Rebuild failed:
**ERROR** 2019/07/12 23:38:30 **"/path/to/layouts/t/code.html:21:1"** : parse failed: template: t/code.html:21: bad character U+005B '['
...
```

---

<div class="post-metadata">

**Author:** ![holehan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/holehan/32/5121_2.png) [@holehan](https://discourse.gohugo.io/u/holehan)\
**Post date:** [July 12, 2019, 2:43pm UTC](https://discourse.gohugo.io/t/add-new-key-value-to-a-map/19607/6 "2019-07-12T14:43:09Z")

</div>

Just a quick reminder (for me as well) from the docs:

> In Go templates, you can’t access array, slice, or map elements directly the same way you would in Go. For example, `$.Site.Data.authors[.Params.authorkey]` isn’t supported syntax.

> Instead, you have to use `index`, a function that handles the lookup for you.

> **[collections.Index](https://gohugo.io/functions/collections/indexfunction/)**
>
> Looks up the index(es) or key(s) of the data structure passed into it.
