'jq: limitting all arrays in a complex json

Trying the following code:

echo '["a","b","c","d","e"]' | jq '..|= if type == "array" then [limit(3;.[])] else . end'
[
  "a",
  "b",
  "c",
  null,
  null
]

Expect it to produce short array:

[
  "a",
  "b",
  "c"
]

JSONs I work with are big and complex. My goal is to shrink all arrays down to length 3. What I get is arrays of original length with lots of nulls.

Ideas?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source