The “invalid numeric literal” error in jq occurs when you try to parse a value as a number, but it doesn’t meet the requirements of a valid numeric literal. To understand this error better, let’s break down the possible causes and provide examples for each scenario.
Possible causes of the “invalid numeric literal” error:
-
Leading or trailing whitespace: If there are spaces or tabs before or after the numeric value, the parser will raise this error.
Example 1: -
Decimal point placement: Numbers in JSON require the decimal point to be followed by at least one digit. If the decimal point doesn’t have any trailing digits, the parser will throw an error.
Example 2: -
Incorrect exponent notation: If you’re working with scientific notation, make sure the exponent part is correctly represented. The exponent should start with either “e” or “E” and be followed by an optional sign (+/-) and at least one digit.
Example 3:
$ echo ' 42' | jq
In this case, the leading space will result in an “invalid numeric literal” error.
$ echo '42.' | jq
This example will result in an “invalid numeric literal” error because there are no digits after the decimal point.
$ echo '42e+' | jq
This example will throw an “invalid numeric literal” error because the exponent is not properly represented.
Conclusion:
The “invalid numeric literal” error in jq can occur due to various reasons such as leading/trailing whitespace, incorrect decimal placement, or incorrect exponent notation. It is essential to ensure that your numeric values adhere to the JSON numeric literal requirements to avoid encountering this error.