Jq: output filenames when filtering multiple input files

Created on 2 Apr 2015  路  7Comments  路  Source: stedolan/jq

When I do jq 'whatever' *.json I can't easily identify which lines of the output were produced by each input file. I'd like to see an option (possibly on by default) to add something like "file1.json:" before each section.

feature request

Most helpful comment

@vito-c - I think input_filename is more relevant here. It's in jq 1.5 and is easy to use. (See e.g. https://stedolan.github.io/jq/manual).

All 7 comments

Perhaps we could have $source variable that names the source of the
current input.

The workaround (in bash, for example) is to proceed like so:

for f in *.json
do
  jq --arg file "$f" 'whatever' "$f"
done

where references to $file in 'whatever' will have the relevant file name.

In fact, I think this workaround is so good that if an "enhancement" is needed, it should wait until after jq 1.5 is released.

Fixed in #753.

@pkoppstein the problem with the workaround you illustrated is what happens when I am trying to actually run a filter across a group of json files. So for example aggregating objects into an array and I want to know where each object came from.

A way to handle this could be to create an interim json object with the filename added.

alljson=""
for f in *.json
do
  alljson+=$(jq --arg file "$f" '{"data":.,"file":$file}' "$f")
done

I'm really not sure how to use $__loc__ to get the file and line number.

@vito-c - I think input_filename is more relevant here. It's in jq 1.5 and is easy to use. (See e.g. https://stedolan.github.io/jq/manual).

@pkoppstein I didn't see that one! so good :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sonots picture sonots  路  3Comments

kelchy picture kelchy  路  4Comments

neowulf picture neowulf  路  3Comments

benjamin-bin-shen picture benjamin-bin-shen  路  3Comments

rokka-n picture rokka-n  路  4Comments