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 · 5 comments

Comments

@nathnolt
Copy link

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Author

commented Feb 9, 2019

Will do, thanks.

@bep

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Author

commented Feb 9, 2019

Yeah That works.
Thanks for your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.