-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Invalid Numeric Literal" on colon following a single-quoted string (incorrect parser error message) #501
Comments
@bartgrantham - Later versions produce the same (confusing) error message, e.g.
The simplest demonstration of the issue may be:
|
This is a very long-standing issue with the JSON parser (not the parser for jq code, but the parser for JSON input). Essentially, the logic is: is it an array? nope. is it an object? nope. (...) well, it must be a number. parse error on a number. I suppose the fix is probably to choose a different error message if the invalid character is not in [0-9+-]. |
according to json.org a string is to be "wrapped in double quotes" so, use double and escape from $ echo "{\"foo\":\"bar\"}" | jq .foo
"bar" |
We've found the best practice to be single quotes around json data and also $ echo '{"foo":"bar"}' | jq '.foo' It allows you to use double quotes without escaping and prevents the shell Also, 1.4 should exist as precompiled binaries on the website and is also On Sun, Nov 23, 2014, 11:44 Jacob Berger [email protected] wrote:
|
@bergerjac, @wtlangford - I know single-quotes for strings are wrong, I mentioned at the beginning that I "absent-mindedly and incorrectly" used them. The issue is that the error produced is cryptic. |
Good point. I might look into a better error for the parser, but the issue On Sun, Nov 23, 2014, 12:29 Bart Grantham [email protected] wrote:
|
Of course, the Windows We really need something like a REPL to make Windows jq users happy... |
Ok, I have a big json dump (sql export) that gives this error. Any work-arounds to make it work with jq until this error is fixed? |
@delip - Since you have a big JSON dump, you are presumably reading it in as a file. In that case, from what you write, it would seem that the error message you see will give the line and column numbers of the invalid JSON. Thus it is unclear to me what kind of work-around you would like. That is, if your big json dump is invalid JSON, the simplest thing to do might be to fix it. |
@delip You have to give us an example. If the request is to have more sensible error messages from the JSON parser, that's another story. |
@nicowilliams, I know! I said it in the opening post that it was "incorrect", the bug is about the error messages being misleading. I even wrote it again in this thread, barely a page up from your comment:
|
Heheh, sorry @bartgrantham. |
PLS use cat /tmp/data.log | jq . |
Your pasted output from the preliminary command works for me when I pipe it
into your jq command. I suspect `/tmp/read` may be doing something
different when it's piped into jq? Try `echo "42fdfc072112624f" |
/tmp/read | tee test.out` and see what's in test.out?
…On Mon, Jun 11, 2018 at 9:55 AM mh-cbon ***@***.***> wrote:
hey guys, i m facing the same issue, but i don t understand.
Please take a look
$ echo "42fdfc072112624f" | /tmp/read | jq -r '.["42fdfc072112624f"]'
parse error: Invalid numeric literal at line 1, column 9
where preliminary command output is
$ echo "42fdfc072112624f" | /tmp/read
{
"42fdfc072112624f": "65465Uv38ByGCZU1WP18P"
}
????????
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#501 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ADQ4VyStuKNUas8FzoXl8W28hHv_YT0_ks5t7nbSgaJpZM4CQUNi>
.
|
Great! |
How i can update value of existing key using jw in shell script |
This error came up for me using |
@simesy That's because you're sending the content of If you really want to redirect the output of your shell script into jq with input redirection, you can do it in bash with |
whoo use curl then parse to jq like 'curl http://req.url/get | jq .' then get error like on top. just add -s on curl, like ' curl -s http://req.url/get | jq .' and everything be alright |
Curious why this was closed? v1.6 still outputs the same nonsense error if the input is not valid JSON.
|
@chet-manley yeah, I think maybe @wtlangford closed this by accident thinking it was a different bug report? This should be re-opened IMO.
|
Should definitely be re-opened. |
Issue #501 has AGAIN been closed incorrectly, after apparently being incorrectly cross-linked with unrelated issues. This issue is about the misleading error message "Invalid numeric literal" when the error is the use of a single quote.
|
Can someone please reopen. This issue has not been adressed. A few more years and we can celebrate a decade. |
I am also getting this in jq 1.6 |
I have a payload (a certificate actually that is now destroyed) that is this:
And if I try to read it, with this command it will error:
Is this a bug or am I doing it wrong? |
@queglay - This is unrelated to this issue. As for your problem, I put the JSON in a file and was able to extract and base64-decode it properly with: I would suggest being very careful with a shell construction like this. You're piping an opaque blob of base64-encoded data through |
Interestingly, I can perform the operation without issue in a shell, but if run during a user data script on boot in ubuntu 18 on ec2, then the error crops up, so I can't reproduce it adhoc. Thanks for sharing your concern re the potential security bug. I have high confidence over the origin of both values, but honestly hadn't considered what you mention here. For my education sake if someone wanted to do harm by manipulation of $target_path what would an example be? |
Maybe do
|
@queglay - I have verified that with your JSON, For future reference, please ask usage questions related to jq at stackoverflow.com using the jq tag: |
I solved my issue by using printf instead of echo |
You can do something like |
This is old and may be referred somewhere else, but I got this message because the input was UTF-16. In my case, to me that was not self evident... HTH |
#!/usr/bin/env python3
import sys
import json
import ast
def convert_to_json(input_str):
# Safely evaluate string to Python object
try:
python_dict = ast.literal_eval(input_str)
return json.dumps(python_dict)
except (SyntaxError, ValueError) as e:
return json.dumps({"error": f"Failed to parse input: {str(e)}"})
if __name__ == "__main__":
# Read from stdin
input_data = sys.stdin.read().strip()
# Convert and print to stdout
print(convert_to_json(input_data)) |
@A1vinSmith not sure i follow the relation to this issue. this covert python ast to json? |
Hi @wader, based on comments from previous discussions, this script addresses the issues that were raised. It can also be a feature of jq, like echo "{'foo':'bar'}" | jq
jq: parse error: Invalid numeric literal at line 1, column 7
vi convert_to_json.py
chmod +x convert_to_json.py
echo "{'foo':'bar'}" | ./convert_to_json.py | jq
{
"foo": "bar"
} |
Your example uses truly invalid JSON, and is not what this issue is about. |
Just use $ echo "{'foo':'bar'}" | tr "\"'" "'\"" | jq
{
"foo": "bar"
} |
Hi Chet, Do you see the first post under the thread? #501 (comment)
|
Your solution is not relevant to the issue being raised, and it even says so in the edit. The JSON is invalid on purpose to showcase the nonsensical error message raised. We are not attempting to solve parsing invalid JSON, only the error message produced. Invalid JSON should never be parsable, by design. |
I'd like to emphasize this. If the "JSON" is malformed, it's not JSON. IMHO jq absolutely should not have a feature where it supports invalid JSON by some kind of transformation. The great value of JSON is that it's minimal and rigorously defined, compromising on that is a mistake. |
Unrelated to this issue but jq has text manipulation support and a $ echo "{'foo':'bar'}" | jq -R "gsub(\"'\"; \"\\\"\") | fromjson"
{
"foo": "bar"
} |
Glorious day! The decade-old issue is resolved! |
EDIT (20150416): I KNOW IT'S INVALID JSON. THE BUG IS ABOUT THE NONSENSICAL ERROR MESSAGE
I get this from absent-mindedly and incorrectly using single-quotes:
This is under 1.3. Trying to reproduce under 1.4, but after 5 minutes of trying to build from source I'm just going to submit this and move on.
The text was updated successfully, but these errors were encountered: