jqlang / jq

Command-line JSON processor
https://jqlang.github.io/jq/
Other
30.39k stars 1.57k forks source link

A newline character is always added to the output #787

Closed niekoost closed 7 years ago

niekoost commented 9 years ago

Problem jq always outputs an extra newline, even when -r (raw output) or -c (compact output) are used. This is nice from a visual perspective when you're using it from a command line, but not when you only want the output for further processing in, for instance, a PHP script.

Example input: "[{},{}]" command: jq '. | length' -c output: "2 " expected output: "2"

Request Please provide an option to suppress the additional new line.

Context: I use jq as a processor for Mandrill events in my PHP scripts. These events are provided in JSON, representing an array of event objects. Since these events also contain the raw message (which can be very large), I first check some fields to determine how to further process each event. I use proc_open.

nicowilliams commented 9 years ago

This is only safe in raw output mode! And it's provided now in master (see -j).

In non-raw output mode jq could safely skip the newline when the value to be printed is an array, object, or string.

nicowilliams commented 9 years ago

This should be implemented by having -j not emit the newline in non---raw-output mode when the value last output is an array, object, or string.

joonas-fi commented 8 years ago

I ran into this as well. I am selecting a single string from a JSON file, and was expecting raw mode to not end up with a newline. I am using jq to extract a string to a shell variable (foo=$(jq ...)). Using $ jq .... | tr -d '\n' is not acceptable generally (was in my case) because it removes all newlines - not just the last one.

nicowilliams commented 7 years ago

I think we can probably have this work such that if you use -j twice then no newline is output ever, but in non-raw output mode it will output a space after any number, boolean, or null.

nicowilliams commented 7 years ago

Actually, in 1.5 it's already the case that you can use -j to stop newlines being printed, but it does put jq into raw-output mode.

mig82 commented 5 years ago

I'm using JQ to define a Git filter for JSON files and this -j flag is what I was looking for. Super-useful.

Kiemes commented 1 year ago

Would have saved me quite some time if this would be in jq --help directly. My bad. I tend to not read every last line of CLI output ;) (meaning See the manpage for more options). Thanks for the option though! Very helpful