Note that none of the parts of your pipeline, downstream from cat, has access to the original file's name. From cat onward, it's just data consisting of the contents of one or several files that arrives on the pipe.
I believe that the following may be something like what you're looking for:
find . -type f -name package.json \
-exec jq -r --arg pkg 'the-package-i-am-looking-for' \
'.dependencies | .[$pkg] // empty | [input_filename,$pkg,.] | @tsv' {} +
This outputs a tab-delimited list consisting of three fields:
- The pathname of the file where the package was found.
- The package name.
- The version string.
The find command calls jq with batches of found pathnames to produce the output, so that as few jq invocations as possible are done. It's the {} + at the end of -exec that will be replaced by the collected filenames.
The jq call produces the output by parsing the input file(s), extracting the .dependencies value, getting the version string for the key corresponding to the package we're looking for, and if something was found, outputting the current filename, the package name that we are querying with, and the version string itself, as a tab-delimited list using @tsv.
The output format could be modified to suit your needs. You could change @tsv into @csv to get CSV-formatted output.
Example with copy of the provided file (with formatting issus corrected) in the current directory and in the directory called dir:
$ tree
.
|-- dir
| `-- package.json
`-- package.json
1 directory, 2 files
$ find . -type f -name package.json -exec jq -r --arg pkg 'the-package-i-am-looking-for' -e '.dependencies | .[$pkg] // empty | [input_filename,$pkg,.] | @tsv' {} +
./package.json the-package-i-am-looking-for 1.2.3
./dir/package.json the-package-i-am-looking-for 1.2.3
If you felt like you really need a pipeline between find and xargs, you could use the following non-portable code:
find . -type f -name package.json -print0 |
xargs -0 \
jq -r --arg pkg 'the-package-i-am-looking-for' \
'.dependencies | .[$pkg] // empty | [input_filename,$pkg,.] | @tsv'
You gain nothing by doing it this way compared to doing it all from find directly.