Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In jq, why doesn’t `.a |= try sort` work?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

It’s a bug in JQ 1.6, first reported here in 2019.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading