'Why sub() can not perform a string substitution?

I don't understand why the sub() command does not replace "%s" with "my string" in the first jq command below. How to make it work?

$ jq -r --arg format '|%s|' '$format | sub("%s"; .desc)' <<< '{"desc": "my string"}'
||
$ jq -r --arg format '|%s|' '$format | sub("%s"; "my string")' <<< '{"x": "y"}'
|my string|
$ jq -r .desc <<< '{"desc": "my string"}'
my string
jq


Solution 1:[1]

You have lost the input context. Save it in a variable (eg. . as $dot) to reference it later (eg. $dot.desc):

$ jq -r --arg format '|%s|' '. as $dot | $format | sub("%s"; $dot.desc)' <<< '{"desc": "my string"}'
|my string|

Solution 2:[2]

You can use null input -n :

jq -nr --arg format '|%s|' '$format | sub("%s"; input.desc)' <<< '{"desc": "my string"}'

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 pmf
Solution 2 Philippe