Unmarshall error

I’m getting an error when trying to unmarshal or remarshal some JSON data.

The data looks like this:

[
   "35acd975-9708-4536-9aa6-2d81893070dd",
   "755633be-0dd7-4a88-8bee-91f3742eeb5a"
]

I’m getting the error: error calling unmarshal: parse error on line 2, column 5: bare " in non-quoted-field and I get the same error with remarshal.

My best guess is Hugo is interpreting this as CSV and the quotes aren’t accepted.
Is there any way to set the data type?

No, you cannot set the format manually.

This…

[
"35acd975-9708-4536-9aa6-2d81893070dd",
"755633be-0dd7-4a88-8bee-91f3742eeb5a"
]

…is valid YAML and valid JSON. We have no way of knowing which, so we don’t even bother looking for the [ character at all when auto-detecting the format.

Adding a format option would work when you know the format, but it would be better if we could handle it automatically.

Checking for a [ to auto-select JSON falls down when:

  • TOML has a [ in a quoted key that appears before a = (fairly common that it’s the first character)
  • YAML has a [ before a : as noted above
  • YAML has a [ in a quoted key that appears before a : (unlikely)
  • CSV has a [ before the delimiter (mildly likely)

So I guess we’d have to add a format option.

This works great:

{{ $s := `
[
  "35acd975-9708-4536-9aa6-2d81893070dd",
  "755633be-0dd7-4a88-8bee-91f3742eeb5a"
]
`}}

{{ $data := "" }}
{{ with $s | resources.FromString "foo.json"  }}
  {{ with transform.Unmarshal . }}
    {{ $data = . }}
  {{ end }}
{{ end }}

{{ range $data }}
  {{ . }}<br>
{{ end }}

Because were naming the resource “foo.json”, transform.Unmarshal sees the “json” extension and knows what to do. No ambiguity.

1 Like