KC14
KC14

Reputation: 47

Trying to filter an array output with jq

I have the given input as such:

[{
  "ciAttributes": {
    "entries": "{\"hostname-cdc1.website.com\":[\"127.0.0.1\"],\"hostname-cdc1-extension.website.com\":[\"127.0.0.1\"]}"
  },
  "ciAttributes": {
    "entries": "{\"hostname-dfw1.website.com\":[\"127.0.0.1\"],\"hostname-dfw1-extension.website.com\":[\"127.0.0.1\"]}"
  },
  "ciAttributes": {
    "entries": "{\"hostname-cdc2.website.com\":[\"127.0.0.1\"],\"hostname-cdc2-extension.website.com\":[\"127.0.0.1\"]}"
  },
  "ciAttributes": {
    "entries": "{\"hostname-dfw2.website.com\":[\"127.0.0.1\"],\"hostname-dfw2-extension.website.com\":[\"127.0.0.1\"]}"
  },
}]

...and when I execute my jq with the following command (manipulating existing json):

jq '.[].ciAttributes.entries | fromjson | keys | [ { hostname: .[0] }] | add' | jq -s '{ instances: . }'

...I get this output:

{
  "instances": [
    {
      "hostname": "hostname-cdc1.website.com"
    },
    {
      "hostname": "hostname-dfw1.website.com"
    },
    {
      "hostname": "hostname-cdc2.website.com"
    },
    {
      "hostname": "hostname-dfw2.website.com"
    }
  ]
}

My end goal is to only extract "hostnames" that contain "cdc." I've tried playing with the json select expression but I get a syntax error so I'm sure I'm doing something wrong.

Upvotes: 1

Views: 72

Answers (1)

peak
peak

Reputation: 116957

First, there is no need to call jq more than once.

Second, because the main object does not have distinct key names, you would have to use the --stream command-line option.

Third, you could use test to select the hostnames of interest, especially if as seems to be the case, the criterion can most easily be expressed as a regex.

So here in a nutshell is a solution:

Invocation

jq -n --stream -c -f program.jq input.json

program.jq

{instances:
 [inputs
 | select(length==2 and (.[0][-2:] == ["ciAttributes", "entries"]))
 | .[-1]
 | fromjson
 | keys_unsorted[]
 | select(test("cdc.[.]"))]}

Upvotes: 1

Related Questions