Search code examples
jsonnulljqis-empty

Difference between `null` and `no output`


I've encountered some difference between null and nothing, can somebody explain it? As in most languages null is considered/used to represent nothing.

The select is documented to return no output. And adding(ie. +) null to X yields X. Now consider these demonstrative examples(takes no input):

  1. adding nothing

here we have empty object, which we update with nothing:

{} | . |= . + ({} | select (.foo == 123))

which results in

null
  1. adding null

same template but with alternative operator to substitute nothing to null:

{} | . |= . + ({} | select (.foo == 123)//null)

which results in

{}

Can someone explain the difference nothing vs null?


Solution

  • null is just a regular JSON value; and conceptually, it is totally different from the absence of a value, i.e, what you termed nothing. Take a look at these for example (empty is a filter that returns nothing):

    $ jq -n '[null] | length'
    1
    $ jq -n '[empty] | length'
    0
    

    That {} + null returns {} back, and that {} | . |= empty does exactly what del(.) does are merely design choices.