Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove last character from json output using JQ

I have a json that looks like this:

{ 
  "HostedZones": [
      {
          "ResourceRecordSetCount": 2,
          "CallerReference": "test20150527-2",
          "Config": {
              "Comment": "test2",
              "PrivateZone": true
          },
          "Id": "/hostedzone/Z119WBBTVP5WFX",
          "Name": "dev.devx.company.services."
      },
      {
          "ResourceRecordSetCount": 2,
          "CallerReference": "test20150527-1",
          "Config": {
              "Comment": "test",
              "PrivateZone": true
          },
          "Id": "/hostedzone/Z3P5QSUBK4POTI",
          "Name": "test.devx.company.services."
      }
  ],
  "IsTruncated": false,
  "MaxItems": "100"
}

And my goal is to fetch a specific Name (in my case it’s the test.devx.company.services), however the Name field contains an extra "." at the end that I’d like to remove from the output.

This is what I have so far:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

jq --raw-output '.HostedZones[] | select(.Name | test("test")?) | (.Name[:-1] | sub("."; ""))'

The problem with that it is removing the first character from the output also.

So the output currently is: est.devx.company.services (JQ play snippet)

Not sure what I’m doing wrong :/

>Solution :

To always remove the last character, if it contains "test":

jq '(.HostedZones[].Name | select(contains("test"))) |= .[:-1]'

To remove it only if it is a dot:

jq '(.HostedZones[].Name | select(contains("test"))) |= sub(".$"; "")'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading