5 ms·
I think you should just invest the hour or so it takes to learn jq. Yes, it's far from a programming language design marvel. But it covers all of the edge cas
by jrockway 6y ago
I think you should just invest the hour or so it takes to learn jq. Yes, it's far from a programming language design marvel. But it covers all of the edge cases, and once you learn it, you can be very productive. (But, the strategy of "copy paste a oneliner from Stackoverflow the one time a year I need it" isn't going to work.)
I think structured data is so common now, that you have to invest in learning tools for processing it. Personally, I invested the time once, and it saves me every single day. In the past, I would have a question like "which port is this Pod listening on", and write something like "kubectl get pod foo -o yaml | grep port -A 3". Usually you get your answer after manually reading through the false-positives. But with "jq", you can just drive directly to the correct answer: "kubectl get pod foo -o json | jq '.spec.containers[].ports'"
Maybe it's kind of obtuse, but it's worth your time, I promise.
- barrkel 6y agoHow about a tool which output nicely formatted JSON with every line annotated with a jq expression to access that value. But then I squint a little bit at the default gron (not ungron) output, and that's actually what I see.
- monsieurbanana 6y agoBut how do you get that '.spec.containers[].ports'? It seems to me that for your example use case, gron is at least useful to first understand the json structure before making your jq request. And, for simple use cases like this one, enough to replace jq altogether.
- jrockway 6y agoWell, the schema of the JSON is something you have to come up with on your own. I happen to have seen like 8 trillion pod manifests so I know what I'm looking for, but if you don't, you have to figure out the schema in some other way. To reverse engineer something, I usually pipe into keys (jq keys, jq '.spec | keys', jq '.spec.containers[] | keys', etc.) For Kubernetes specifically, "kubectl explain pod", "kubectl explain pod.spec", etc. will help you find what you're looking for.
- seppel 6y ago> Well, the schema of the JSON is something you have to come up with on your own. I happen to have seen like 8 trillion pod manifests so I know what I'm looking for, but if you don't, you have to figure out the schema in some other way. Well, or you just do kubectl get pod pod -o json | gron | grep port and you will get the answer to the original question + the path.
- lenkite 6y agoBetter to learn xidel http://www.videlibri.de/xidel.html http://www.videlibri.de/xidel.html which is standards based and more sane to read.
- bradknowles 6y agoWith respect, it might have been just an hour for you. For me, it was hours and hours and hours and days and days wasted with jq, before I found gron. Not looking back.