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

Number types don't work properly in JSON data files #5685

Closed
nathnolt opened this issue Feb 9, 2019 · 6 comments · Fixed by #5705
Closed

Number types don't work properly in JSON data files #5685

nathnolt opened this issue Feb 9, 2019 · 6 comments · Fixed by #5705
Milestone

Comments

@nathnolt
Copy link

nathnolt commented Feb 9, 2019

I have a simple data file like this:

userdata.json

[
	{
		"name": "John doe",
		"salary": 300
	},
	{
		"name": "Frank foobar",
		"salary": 500
	},
	{
		"name": "Rick foobar",
		"salary": 400
	}
]

and in my layout I'm doing

{{where .Site.Data.userdata "salary" "<" 500 }}

The output is [] but the expected output is [map[salary:300 name:John doe] map[name:Rick foobar salary:400]]

If I have the same thing as a yaml file

---
- name: John doe
  salary: 300
- name: Frank foobar
  salary: 500
- name: Rick foobar
  salary: 400

It does give me the expected output of [map[salary:300 name:John doe] map[name:Rick foobar salary:400]]

I'm not quite proficient in Hugo or go html/templates, so excuse me if this issue has been posted already, but I couldn't find it with my current knowledge.

>hugo version output

Hugo Static Site Generator v0.54.0-B1A82C61 windows/amd64 BuildDate: 2019-02-01T09:42:02Z

@tryzniak
Copy link
Contributor

tryzniak commented Feb 9, 2019

Nice find! It's definitely a bug. Try using {{where .Site.Data.userdata "salary" "<" 500.0 }} (notice 500.0) and it works as expected.

@nathnolt
Copy link
Author

nathnolt commented Feb 9, 2019

Will do, thanks.

@bep
Copy link
Member

bep commented Feb 9, 2019

Yes, we should handle this. I can guess about why this is happening:

  • The JSON decoder (the one that gets shipped with Go) treats all numbers as floats (float64), which I guess does not compare well to an integer in your example (it should)
  • YAML and TOML has a more diverse number type gallery.

@bep
Copy link
Member

bep commented Feb 9, 2019

You could try:

{{where .Site.Data.userdata "salary" "<" 500.0 }}

And see what that gets you.

@bep bep added the Bug label Feb 9, 2019
@bep bep added this to the v0.56 milestone Feb 9, 2019
@nathnolt
Copy link
Author

nathnolt commented Feb 9, 2019

Yeah That works.
Thanks for your time.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants