2

I need a jq filter that would:

  • when given {"a": [3, 1, 2]}, produce {"a": [1, 2, 3]}
  • when given {"b": 0}, produce {"b": 0}

.a |= sort doesn’t work in the second case, because “null (null) cannot be sorted, as it is not an array”.

From the manual I figured .a |= try sort should work. When .a is an array, it should just sort it. When .a is null, try should catch the error from sort and produce nothing, thus deleting "a". This works for {"b": 0}, but for {"a": [3, 1, 2]} it produces {} instead. Why?

I know other ways to accomplish this, but I want to understand why mine doesn’t work.

Vasiliy Faronov
  • 11,840
  • 2
  • 38
  • 49

1 Answers1

2

It's a bug in JQ 1.6, first reported here in 2019, and documented here.

oguz ismail
  • 1
  • 16
  • 47
  • 69
  • 1
    Please note that the bug was fixed in Nov 2019 and has thus been available in the "development" branch for some time now. – peak Oct 24 '22 at 18:58