Monday, 14 March 2022

Port Stripping

 So, like most things we do with tech, there's probably 73 different ways to achieve this particular requirement ...

However, I had a specific need to parse a JSON document, pull out a specific key, which contained an endpoint URL, and extract only the port number.

Here's an example of my JSON document: -

{

    "endpointURL":"https://www.ibm.com:443"

}

and, specifically, I just want to extract the port number of 443.

This is up with what I ended: -

cat server.json | jq -r '.endpointURL' | sed 's/.*://'

so it's three distinct commands, tied together with pipes: -

1. cat server.json to dump out the content of the document

2. jq -r '.endpointURL'  to only pull the endpointURL key

3. sed 's/.*://' to apply a regular expression ( aka RegExp ) to extract the port number

cat server.json | jq -r '.endpointURL' | sed 's/.*://'

443

Nice!

No comments:

Note to self - use kubectl to query images in a pod or deployment

In both cases, we use JSON ... For a deployment, we can do this: - kubectl get deployment foobar --namespace snafu --output jsonpath="{...