-
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
How to skip non-json lines? #884
Comments
Ugly workaround, grep to use only lines starting with "{": the_executable | grep '^{' | jq '.' |
|
You can skip non-objects with:
|
dhcp232-052:.Trash luckydonald$ cat cli_temp.txt | jq -R 'fromjson? | select(type == "object")'
error: syntax error, unexpected '?', expecting $end
fromjson? | select(type == "object") 1 compile error Latest version from homebrew (jq-1.4) |
Also I realized, (in case of grep) it will only output, after the executable is terminated, while grep displayes instantly. |
@luckydonald wrote:
Perhaps in your case using "?" or a simple grep to select JSON entities will work, but both approaches are somewhat brittle in that they will only work reliably if every JSON entity is on a line by itself. If on the other hand that assumption cannot be made, then focusing on weeding out the error messages might well be the way to go. The key here is that newlines are not allowed inside JSON strings. This makes it quite likely that "grep -v" will be your friend. |
Version from |
FWIW, I came up with this to display pretty JSON lines mixed with regular text lines:
|
@chesshacker - Nice example. As far as the documentation is concerned, the use of try/catch in this context is already documented in the FAQ (https://github.com/stedolan/jq/wiki/FAQ) -- search for |
@luckydonald You need to use grep with |
If it's any help, this function works wonders for me: # Use `jq` with both JSON and non-JSON lines.
function jjq {
jq -R -r "${1:-.} as \$line | try fromjson catch \$line"
} |
I took a simpler approach and just filtered bad lines out with grep
|
Sed helped me, ansible facts input example, not same as the input talked about here, but is a good example. |
The situation:
I am getting input, piped in from another program.
Sometimes there are error messages, which are no valid json.
The next line there will be valid json (or another error) again.
So how can I say "discard the line (ending with \n) and continue with the next one"?
How can I ignore all lines which
failes to parse?
The closest I got was
.|split("\n")[]| fromjson.example
with Raw Input enabled.It at least processed all lines, and only raises errors on the non-json lines.
Still I don't found a way how to surpress the errors, and get the correct lines printed again.
The text was updated successfully, but these errors were encountered: