-
-
Notifications
You must be signed in to change notification settings - Fork 320
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
Add "raw" flag equivalent for JQ #125
Comments
Nice suggestion, @LorisFriedel! Can you give a brief example of a program like this, showing how you'd like this behaviour to work? |
For example, I would ideally do this to find a single element in a json object and use it directly: nodeName, err := script.
Exec("kubectl get pod myPod -o json").
JQRaw(".spec.nodeName").
String()
// output would be: xxx-ffkch-something-e32as-someregion-gsbn4 Today, that's what I need to do: nodeName, err := script.
Exec("kubectl get pod myPod -o json").
JQ(".spec.nodeName").
String()
// output is: "xxx-ffkch-something-e32as-someregion-gsbn4"\n
nodeName = strings.Trim(strings.TrimSpace(nodeName), "\"") // extra step to sanitze the output
// output is then: xxx-ffkch-something-e32as-someregion-gsbn4 In fact the trailing "\n" is almost always a bit of a pain to handle, not really related to JQ :/ |
Maybe this could be done with a more general-purpose trim method—something like nodeName, err := script.
Exec("kubectl get pod myPod -o json").
JQ(".spec.nodeName").
Trim().
String()
// output would be: xxx-ffkch-something-e32as-someregion-gsbn4 |
This could be, but the trim would need to strip spaces AND quotes, and that would make it a weird "Trim" method I guess, WDYT? |
Maybe there's a better name for this... |
Maybe just a |
I'd like to gather a bit more data on exactly how people are using |
Hi @bitfield,
I cannot speak for every jq user out there however, using jq with the Update: That said, given your proposal in the comments #125 (comment) and #125 (comment), perhaps we could add Trim(), we could also add good 'ol Tr() (man 1 tr) to keep it close to the shell scripting as much as possible. |
I believe you, but we need a realistic example to use as the input to the design process. |
Hi @bitfield,
Thinking about it some more... I wouldn't do it. Adding some convenience functions like Trim() or Strip(), and also Tr() might be a better alternative - new API could be easily reused somewhere else, other than removing bloat from jq's output. |
Anything like Trim(), Strip() and such would save a lot a painful code handling those polluted outputs indeed! |
Is there any example you can think of where a 'trim spaces and quotes' method would be used with anything other than |
For the "trim quote" part, not really, but for the "trim space" part I guess everywhere where |
Hi @bitfield,
I think, if anything, we would want the following:
The Trim() can be used as part of the pipeline and such to sanitise whitespaces - both leading and trailing at once. Perhaps a specialised variety could be added too if it at all makes sense, so that would be TripLeading() and TrimTrailing(), but I am not sure if we need this (an open question here). An example use case (aside from using it for JQ): working with a result of a process spawned with Exec() to clean up output from the external process. The Tr(), that could be used to strip anything... anything you see fit from the pipeline. For example: script.Exec(`echo 'a" b`).Tr(`" `, "").Stdout() Would produce: ab This would be akin to running |
The problem I see is that a Conversely, a It's possible that we could get the best of both worlds, by providing a |
Hi @bitfield,
Not if we make it generic. I can see it being useful as a handy function that can be used to rid something in the pipeline, so to speak, of whitespaces and such.
What if we kept it simple? Like String#strip as an example from Ruby.
Or... Tr() and Strip(), simply. Users of JQ can then use either depending on their needs. Unless you do want to have JQ() and JQRaw(). Thoughts? |
Well, it's not just whitespace that needs to be removed from the JQ data; it's quotes, too. A script.Echo(data).JQ(query).Strip().Stdout() On the other hand, suppose there were some method script.Echo(data).JQ(query).Translate(" \"", "").Stdout() ...which is a lot to have to write after every call to As I say, cleaning up JQ data is such a specific task that I can't see a no-argument method to do it being useful for anything else. |
Hi @bitfield,
The idea to add Strip() was not strictly related to JQ(), but more as a generic function (a helper, if you wish) that could be used here and elsewhere to remove leading and trailing whitespaces.
I see your point. OK. To keep it simple and retain the same ergonomics as other functions, introducing JQRaw() next to JQ() might be a good idea then.
The Strip() and Translate() were intended to be as generic as possible so that both could find use in different places too. |
The GoLang |
Right, but if we had (for example) |
I would use |
Yes, understood, but why would you need to do that? In what real-world application would you need to trim specific characters from either end of a string? Don't take this as a challenge—I'm not saying there's no use case for this—but as an invitation to contribute suggestions for ways this feature might be used. If a number of intelligent, experienced people cannot, in practice, think of any situation in which they would really use this feature, then I'm happy to leave the feature out. The best tools tend to be the ones that aren't bloated with unnecessary features. |
Thanks @ John for your great work on script. I am also in the same kind of questions like you guys. On my side I am regularly using jq to quickly strip some json payload. In my case, I am using |
I suspect a simple script.Echo(`{ "ip": "192.168.0.2"}`).JQr(".ip").Stdout()
// Output:
// 192.168.0.2 What do you think? |
Yes but you will add a new function to your script list. I am not sure this is what you want ... Also as I mentioned there are many other parameters that I use like the S (slurp) or the -C for instance. But again, after reading the argument list in gojq, this may be not a good idea to get access to some parameters. So yes why not JQr or JQraw ... Simple and easy. However I also like the idea to have a kinda generic "translator" in your set of functions. Thanks anyway. |
It doesn't look like |
Hello John, I have also the idea to have a generic function in the pipeline of functions. For example : |
You can do exactly this with script.File(path).JQ(".ip .host").FilterLine(func (s string) string {
return strings.Trim(s, `"`)
}.String() |
Hello! Thank you for the amazing work :)
I would like to suggest the possibility to output a string using JQ but with the equivalent of "-r" or "--raw" we can find on the official jq command: this allows to get a clean output for a single field for example, instead of having to sanitize it by doing some triming.
What do you think?
Thanks!
The text was updated successfully, but these errors were encountered: