Skip to content
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 remove quotes from the results? #1735

Closed
ghost opened this issue Oct 8, 2018 · 30 comments
Closed

How to remove quotes from the results? #1735

ghost opened this issue Oct 8, 2018 · 30 comments

Comments

@ghost
Copy link

ghost commented Oct 8, 2018

for example
jq .data
result : "hello"

I want to remove the double quotes in the result. What should I do?

@pkoppstein
Copy link
Contributor

What should I do?

Check the jq manual!

@ghost
Copy link
Author

ghost commented Oct 8, 2018

What should I do?

Check the jq manual!

thanks! i find "-r" or "--raw-output" in the manual..

@ghost ghost closed this as completed Oct 8, 2018
@AcsFL
Copy link

AcsFL commented Feb 14, 2019

@pkoppstein that has to be one of the most unuseful comments in the history of github
@sendtomoon thanks for sharing

@texascloud
Copy link

From the manual (version jq-1.5-1-a5b5cbe installed on ubuntu 16.04)

·   --join-output / -j:

           Like -r but jq won´t print a newline after each output.

Would be useful to you if you're comparing the length of characters from an output to some other number. For instance, when using jq to find a UUIDv4 and checking its length to confirm it's 36 characters, jq -r will include a hidden newline character. Something to keep note of!

@lleeoo
Copy link

lleeoo commented Jul 21, 2020

FTR -r does not work with @CSV, at least in 1.5.

@pkoppstein
Copy link
Contributor

pkoppstein commented Jul 21, 2020

@lleeoo - Please note that the -r command-line option does work as it is intended to, in all extant versions of jq. In particular, when used in conjunction with @csv, the -r option ensures that valid CSV is produced. This is accomplished by unconditionally quoting string values in each CSV line. If you want to eliminate the quotation marks (at the risk of not producing the intended CSV output), use @tsv and then blindly convert the tabs to commas. A more robust approach would be a bit more complicated, but it might not be worth the effort.

@lleeoo
Copy link

lleeoo commented Jul 22, 2020

Thank you, @pkoppstein. I understand that the behaviour is as intended, and FWIW I think it is the correct behaviour at the current stage. I just put that reference here for people who will come to the page via Google. I found this SE answer to be a great summary of the topic. The entire question discussion is useful.

What I think might be helpful is if the string does not need quotes in CSV, then it shouldn't take them, but that is a nice-to-have.

@jossef
Copy link

jossef commented Aug 17, 2020

What should I do?

Check the jq manual!

image

@e1m1
Copy link

e1m1 commented Aug 18, 2020

@sendtomoon thank you!

@godrose
Copy link

godrose commented Oct 7, 2020

What should I do?

Check the jq manual!

That's really rude. I mean it's open source and all that but implementing something with so counter-intuitive approach and saying the users "RTFM" is just a very unpleasant user experience

@aesyondu
Copy link

This is the search term I used on google:

remove unwanted backslashes jq csv

Which showed this github issue as the second result. I fully understand that I should be RTFM. However when I have a deadline or any form of time constraint, or just can't be bothered, and knowing just one specific thing will solve my problem, github issues like these (or stackoverflow questions for that matter) help a lot. Thanks!

@tobixen
Copy link

tobixen commented Oct 15, 2020

I also came here through a popular search engine, well aware that the information I need can be found through the man page, but thinking I could find the answer faster through said search engine. IMO, this is the kind of thing that would fit much better i.e. in stackoverflow rather than as a github issue. Anyway, found the -r-option here thanks to @sendtomoon and happy with that :-)

@DaVince
Copy link

DaVince commented Oct 20, 2020

What should I do?

Check the jq manual!

That's really rude. I mean it's open source and all that but implementing something with so counter-intuitive approach and saying the users "RTFM" is just a very unpleasant user experience

But... it wasn't conveyed in a rude way at all. A helpful pointer was given to where this kind of information is officially provided. If the flag were to change for any reason, the actual manual would be updated, but anyone visiting this thread would be none the wiser.

@vidstige
Copy link

Hold up. How is rude to take the time help a complete stranger with a friendly and accurate pointer? Give a man a fish and he eat for a day, teach him how to fish and you feed him for a lifetime.

The way this issue thread played out everyone has 1) the answer to this specific answer right where you need it in a tight spot and 2) a general way of finding answers yourself. 🤷

@reutsharabani
Copy link

Manuals should be SEO-ed. Then we would not have this problem! 😄

I think most devs usually just google something so simple to get a straight answer instead of trying to find it in the manual. This makes sense because they just want to get it done and google takes significantly less time.

@GwynethLlewelyn
Copy link

GwynethLlewelyn commented Jan 12, 2021

Actually, it's not that easy to locate the necessary option in the manual. I know that it's at the very beginning; but for a complete newbie of jq the wording used to describe the -r/-j options is less than obvious that it does what the OP asked.

I speak for myself here: I had a wrongly written jq filter, and -r did not help much. After figuring out what was wrong, I forgot all about the 'experimenting' with -r until, well, I was pretty sure that I got exactly what I wanted, and then I had to figure out how to remove quotes...

@ramsey
Copy link

ramsey commented Jun 4, 2021

@sendtomoon Thank you for posting the answer here. I checked the man page first, but I would never have thought to look for the word "raw" in a 3,288-line help file. 😄

@mwmahlberg
Copy link

@pkoppstein that has to be one of the most unuseful comments in the history of github

No, that one is. And it is also an undue and unnecessary ad personam.

Give a man a fish and you feed him for a day. Teach a man how to fish, and you feed him for a lifetime.

@migralito
This comment has been minimized.
@marcusmueller
Copy link

marcusmueller commented Oct 27, 2021

However when I have a deadline or any form of time constraint, or just can't be bothered, and knowing just one specific thing will solve my problem, github issues like these (or stackoverflow questions for that matter) help a lot. Thanks!

friendly remark for those considering asking instead of searching: What you're saying to the contributors to FOSS software like jq is that you value their time so much less than your own, that you are willing to skip reading the documentation to take their time for free. Documentation they wrote, so you have something to refer to.

Now, I'm not willing to point fingers here in terms of what is a useful or a polite comment and what could have been phrased differently, but people actively calling maintainers and contributors names in this issue should probably consider at least paying these folks industry-standard consulting rates for their time spent answering such non-issues. (@migralito, that means you; you're not only rude, but racist on top...)

@MECJUMP
Copy link

MECJUMP commented Feb 4, 2022

I have used command -r for this JSON and I am still getting this output with double quotes. See example below:

input JSON ( $payload ) ->
{
"dataset": "zip code",
"zipCodes": [
"03788",
"31713",
"68167"
]

}

bash.sh ->

function load_json {

dataset=$(echo "$payload" | jq --raw-output .dataset)
zip_codes=$(echo "$payload" | jq --raw-output .zipCodes)

echo "dataset -> $dataset"
echo "zipcodes -> $zip_codes"

}

OUTPUT ->

dataset -> zip code
zipcodes -> [
"03788",
"31713",
"68167"
]

As showed in the example, I get zip code without quotes but I can't eliminate double quotes in the zipcodes array. I need to read this information as a filter parameter for a SQL query in postgrest and I will need to pass it with single quotes.
If anyone knows how to solve it, I will appreciate a lot.
Thanks in advance for your help :)

@wader
Copy link
Member

wader commented Feb 4, 2022

--raw-output only applies to string and the "root" value. In this case the root value is an array.

Try this which will iterate the array and output each string in the array separately, that way --raw-output should apply but you won't get any commas or [...] around it:

jq --raw-output .zipCodes[]

If you want that and single quotes can fake it by doing something like:

jq --raw-output '"[", (.zipCodes[] | "\u0027\(.)\u0027,"), "]"'

But that will include an ending "," in the array... needs some more work to get rid of that

@tobias-feil-by
Copy link

The first answer says "check the manual". But this page is the top google result for me.

@MECJUMP
Copy link

MECJUMP commented Feb 8, 2022

@wader Thank you so much for your reply.

At the end, I found a workaround using the command 'tr' from hash

I drop the solution implemented below:

zip_codes=$(echo "$payload" | jq .zipCodes | tr '"' "'" .zipCodes| tr -d '[]' .zipCodes)

and the SQL query where I've used so:

psql "$host/$DATABASE" -c "SELECT countrycode, postalcode FROM geolocation WHERE postalcode IN ($zip_codes)"

The output given in this case is :

zip_codes ->
'03788',
'31713',
'68167'

Hope it is useful for someone.

@wader
Copy link
Member

wader commented Feb 8, 2022

@MECJUMP 👍 no problem, good to remember jq is not the solution to all problems... but almost :)

@gerrywastaken
Copy link

me: Hey English isn't my first language and I'm just wondering what the word is for somebody who isn't very kind?
@pkoppstein: Check the dictionary

@rondomondo
Copy link

rondomondo commented Apr 12, 2022

I was doing something similar - this did it for me - keys with no quotes

cat somefile.json | jq "keys" | jq -r ".[]"

edit - easier way is below from @wader - tnx!

@wader
Copy link
Member

wader commented Apr 12, 2022

@rondomondo Hi, you can add a []to iterate the keys directly, echo '{"a":1, "b":2}' | jq -r 'keys[]'

@sarnobat
Copy link

I wish this worked with yq!

@kpym
Copy link

kpym commented Jun 7, 2023

@lleeoo (three years later, but for the sake of completeness) To remove the quotes from the CSV output (at your own risk), you can use @csv | sub("\"";"";"g").

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests