Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between slurp, null input, and inputs filter

Tags:

performance

jq

Given the input document:

{"a":1}
{"b":2}
{"c":3,"d":4}

What is the difference between the following jq programs (if any)? They all seem to produce the same output.

  1. jq '[., inputs] | map(to_entries[].value)'
  2. jq -n '[inputs] | map(to_entries[].value)'
  3. jq -s 'map(to_entries[].value)'

In other words, the following (simplified/reduced) invocations seem identical:

  • jq '[.,inputs]'
  • jq -n '[inputs]'
  • jq -s '.'.

How are they different? Are there scenarios where one works, but the others don't? Did older versions of jq not support all of them? Is it performance related? Or simply a matter of readability and personal preference?


Bonus points (added later to the question): does the same hold true for the following programs?

  1. jq '., inputs | to_entries[].value'
  2. jq -n 'inputs | to_entries[].value'
  3. jq -s '.[] | to_entries[].value'
  4. jq 'to_entries[].value'
like image 551
knittl Avatar asked Sep 03 '25 09:09

knittl


1 Answers

With jq '-n [inputs] ....' and jq '[.,inputs] ....', you are loading the whole file into memory.

A more memory-efficient way to achieve the result as an array is:

jq -n '[inputs | to_entries[].value]'
like image 189
Philippe Avatar answered Sep 05 '25 01:09

Philippe