hashicorp / hashicorp/consul-template

Unable to access nested keys when reading JSON from file

Open
#1,313 3 comments 0 reactions 0 assignees View on GitHub
docs
Dominant language
Go
Stars
4.8k
Forks
801
Avg merge
4h 5m
Merged PRs (30d)
6

Description

### Consul Template version

`consul-template v0.23.0 (5cf9d687)`

### `cfg1.tpl` (working)

```liquid
{{ define "some-json" }}
{
"ds": {
"meta-data": {
"local-ipv4": "127.0.0.1"
}
}
}
{{ end }}

{{- $id := executeTemplate "some-json" | parseJSON -}}

{{- index (index $id.ds "meta-data") "local-ipv4" -}}
```

`consul-template -dry -once -template=cfg1.tpl` works as expected, returning `127.0.0.1`.

### Configuration 2 (fails)

`consul-template -dry -once -template=cfg2.tpl` fails with:

```
2019/11/15 11:44:37.457560 [ERR] (cli) cfg2.tpl: execute: template: :2:10: executing "" at : error calling index: index of untyped nil
```

#### `cfg2.tpl`

```liquid
{{ $id := file "instance-data.json" | parseJSON }}
{{ index (index $id.ds "meta-data") "local-ipv4" }}
```

#### `instance-data.json`

(same as `some-json` template above)

```json
{
"ds": {
"meta-data": {
"local-ipv4": "127.0.0.1"
}
}
}
```

### Configuration 3 (fails)

`consul-template -dry -once -template=cfg3.tpl` fails with:

```
2019/11/15 11:39:57.817484 [ERR] (cli) cfg3.tpl: execute: template: :7:11: executing "" at : error calling index: index of untyped nil
```

#### `cfg3.tpl`

```liquid
{{ define "file-json" -}}
{{ file "instance-data.json" }}
{{- end }}

{{- $id := executeTemplate "file-json" | parseJSON -}}

{{- index (index $id.ds "meta-data") "local-ipv4" -}}
```

#### `instance-data.json`

As above.

### Expected behavior

Should have gotten same output regardless of source of JSON.

### Actual behavior

Ability to access JSON-parsed map structure is dependent upon source of JSON.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with cfg1.tpl, cfg2.tpl, and cfg3.tpl, then inspect the implementations of file, executeTemplate, and parseJSON. Compare the values passed through each path and verify that nested keys in instance-data.json produce 127.0.0.1 regardless of the JSON source.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.