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

from_entries of [] is null, should be {} #280

Closed
rvedotrc opened this issue Jan 27, 2014 · 5 comments
Closed

from_entries of [] is null, should be {} #280

rvedotrc opened this issue Jan 27, 2014 · 5 comments
Labels
Milestone

Comments

@rvedotrc
Copy link

jq version 1.3

rachel@dudley stanley$ echo '[]' | jq -M 'from_entries'
null
rachel@dudley stanley$

For consistency, I expected {}.

The man page doesn't say anything about this inconsistency, so it sounds like a bug.

@nicowilliams
Copy link
Contributor

I too think it's a bug. from_entries is implemented like this:

def from_entries: map({(.key): .value}) | add;

and add is implemented like this:

def add: reduce .[] as $x (null; . + $x);

The problem is the null in add as the initial reduce value; changing it to '{}' fixes the problem. I think we need a version of add with a default initial value to pass to reduce, then make from_entries use that with {}.

In the meantime you can work around this by defining your own from_entries, or by replacing nulls output by it with {}.

@nicowilliams
Copy link
Contributor

I.e., the workaround should be to add this to your program:

def from_entries: map({(.key): .value}) | reduce .[] as $x ({}; . + $x);

That will probably be the fix too (in builtin.c).

@rvedotrc
Copy link
Author

Or more simply, if I understand correctly:

"def from_entries: map({(.key): .value}) | add | .//={};",

@nicowilliams
Copy link
Contributor

Even def from_entries2: from_entries|.//{};.

@nicowilliams
Copy link
Contributor

Oops, I missed this for the 1.4 release.

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

No branches or pull requests

2 participants