jq icon indicating copy to clipboard operation
jq copied to clipboard

Enhancement: Include the file name in parse error messaging

Open GreenGremlin opened this issue 7 years ago • 7 comments
trafficstars

It would be helpful if the filename were included in parse error messages. While this could fairly easily be handled by a script, it would be nice if this functionality was included in jq.

Example script

#!/usr/bin/env bash

set -e

for file in data/*.json; do
    some_data=$(jq . file)
done

Currently, a parse error produces the following output.

$ ./parse_data.sh
parse error: Expected another key-value pair at line 20, column 3

It would be helpful if it output something like this.

$ ./parse_data.sh
Error parsing file data/2017-10-data.json: Expected another key-value pair at line 20, column 3

Even better, this feature could be added behind a --verbose flag and include the context of the error.

$ ./parse_data.sh
Error parsing file data/2017-10-data.json:

  19 |    "name": "Bob Smith",
> 20 |   },
        ^
  21 |   {

Expected another key-value pair at line 20, column 3

GreenGremlin avatar Dec 12 '17 23:12 GreenGremlin

I believe this is already so in master. 1.6 is coming.

nicowilliams avatar Dec 12 '17 23:12 nicowilliams

+1 for this enhancement 😄

Even on jq-1.6 (as reported by jq --version), parse errors still return the same filename-agnostic message.

chbrown avatar Mar 05 '19 19:03 chbrown

Still an issue in 2023. There should be a switch that includes the full file path in the erorr message.

This is especially a pain in the rear when processing thousands of small json files with jq in parallel fashion.

bf avatar May 07 '23 12:05 bf

7 years later... still not implemented 🤦‍♂️

reythia avatar Jun 30 '24 11:06 reythia

🤦‍♂️

emanuele6 avatar Jun 30 '24 13:06 emanuele6

Did a draft PR #3146

I wonder what @nicowilliams referred to about as this being fixed in master? maybe reporting of current name:line of input on uncaught error? that is the only place currently where i see jq_util_input_get_position is used

wader avatar Jun 30 '24 14:06 wader

I wonder what @nicowilliams referred to about as this being fixed in master?

Here's an example output of calling jq 1.6 over thousands of JSON files:

...
parse error: Invalid numeric literal at line 1, column 5
parse error: Invalid numeric literal at line 1, column 16
parse error: Invalid numeric literal at line 1, column 16
parse error: Invalid numeric literal at line 1, column 16
parse error: Invalid numeric literal at line 1, column 16
parse error: Invalid literal at line 1, column 5
parse error: Invalid numeric literal at line 1, column 16
parse error: Invalid numeric literal at line 1, column 2
parse error: Invalid numeric literal at line 1, column 2
parse error: Invalid numeric literal at line 1, column 5
parse error: Invalid numeric literal at line 1, column 5
parse error: Invalid numeric literal at line 1, column 5
jq: error (at ./573455af-5842-48b7-9add-7a7c0349b502.json:260): Cannot index array with string "foo"
parse error: Invalid numeric literal at line 1, column 5
parse error: Invalid numeric literal at line 1, column 2
parse error: Invalid literal at line 1, column 5
parse error: Invalid literal at line 1, column 5
parse error: Invalid literal at line 1, column 5
parse error: Invalid literal at line 1, column 5
parse error: Invalid numeric literal at line 1, column 15
parse error: Invalid numeric literal at line 1, column 2
parse error: Invalid numeric literal at line 1, column 5
parse error: Invalid numeric literal at line 1, column 2
parse error: Invalid numeric literal at line 1, column 2
parse error: Invalid numeric literal at line 1, column 4
parse error: Invalid numeric literal at line 2, column 0
parse error: Invalid numeric literal at line 1, column 3
parse error: Invalid numeric literal at line 1, column 2
parse error: Invalid numeric literal at line 1, column 15
parse error: Invalid numeric literal at line 2, column 0
parse error: Invalid numeric literal at line 1, column 2
parse error: Invalid literal at line 1, column 5
parse error: Invalid numeric literal at line 1, column 2
parse error: Invalid numeric literal at line 1, column 3
parse error: Invalid numeric literal at line 1, column 2
parse error: Invalid numeric literal at line 1, column 2
parse error: Invalid numeric literal at line 1, column 3
...

...so there is definitely some type of error which shows the filename!

alxndrsn avatar Jul 04 '24 06:07 alxndrsn