Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse Json Array issue: Unable to unmarshal object #924

Closed
NicoHood opened this issue Aug 15, 2020 · 5 comments · Fixed by #926
Closed

Parse Json Array issue: Unable to unmarshal object #924

NicoHood opened this issue Aug 15, 2020 · 5 comments · Fixed by #926
Labels

Comments

@NicoHood
Copy link

Hi,
I am not sure if this is an issue or if I just did something obvious wrong. Maybe you can help and other will see the post if they run into the same issue.

test.json

["London", "Johannesburg", "Windhoek"]

test.tmpl

{{ range $i, $city := (ds "cities") }}
{{ add 1 $i }}: {{ include "weather" (print $city "?0") }}
{{ end }}

command:

$ gomplate -d 'cities=test.json'  -d 'weather=https://wttr.in/?0' -H 'weather=User-Agent: curl' -f test.tmpl 

23:41:03 FTL  error="template: test.tmpl:1:23: executing \"test.tmpl\" at <ds \"cities\">: error calling ds: Unable to unmarshal object [\"London\", \"Johannesburg\", \"Windhoek\"]\n: yaml: unmarshal errors:\n  line 1: cannot unmarshal !!seq into map[string]interface {}"

It works if you use city: [...] in json and (ds "cities").city inside the template. But my json root object is not dict, it is an array, because I request it via REST api (common usecase).

Thanks :-)

@hairyhenderson
Copy link
Owner

Hi @NicoHood - thanks for logging this!

.json files are assumed to be of type application/json which gomplate interprets as a JSON object. If you want, you can override the type to application/array+json (which is a non-standard MIME type that gomplate understands) by appending ?type=application/array+json to the datasource's URL:

$ gomplate -d 'cities=test.json?type=application/array+json'  -d 'weather=https://wttr.in/?0' -H 'weather=User-Agent: curl' -f test.tmpl

1: Weather report: London

      \   /     Sunny
       .-.      20 °C
    ― (   ) ―   ↑ 15 km/h
       `-’      10 km
      /   \     0.4 mm


2: Weather report: Johannesburg

      \   /     Clear
       .-.      11..13 °C
    ― (   ) ―   ↓ 17 km/h
       `-’      10 km
      /   \     0.0 mm


3: Weather report: Windhoek

      \   /     Clear
       .-.      14 °C
    ― (   ) ―   ↑ 12 km/h
       `-’      10 km
      /   \     0.0 mm

See https://docs.gomplate.ca/datasources/#mime-types for more details, and especially https://docs.gomplate.ca/datasources/#overriding-mime-types

@NicoHood
Copy link
Author

NicoHood commented Aug 18, 2020

WTF, there is a special mime type for this!? That was new to me, thanks!

Edit: Maybe add this to your post or readme?

@hairyhenderson
Copy link
Owner

Edit: Maybe add this to your post or readme?

@NicoHood which post? which README? It's in the docs, as I linked.

@NicoHood
Copy link
Author

Oh, my fault. I was referring to this post:
https://blog.hairyhenderson.ca/post/one_template_many_outputs/

It might be useful to add this note there, as it looks like a common usecase to connect a rest api to such a template. I was not aware that there is a special json array mime type, I thought it is all the same.

@hairyhenderson
Copy link
Owner

Oh, my fault. I was referring to this post:
https://blog.hairyhenderson.ca/post/one_template_many_outputs/

Wild - I didn't know anyone read those 😂

It might be useful to add this note there

Not sure that really makes sense... I didn't use a JSON array file as a datasource in that article directly, and where I did mention using datasources, I linked straight to the docs, which is where you'd find the answer.

I was not aware that there is a special json array mime type, I thought it is all the same.

It's is a bit of a hack TBH... JSON suffers from being a bit too general sometimes, and YAML even more so. So it's tough to make the right call when parsing sometimes. However, I have thought in the past about maybe attempting to parse as an array if the file can't be parsed as an object. I'll re-open this and just add a workaround to make this simpler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants