13

My test json file is as follows:

{
   "server":"xxx",
   "cert":"line1\nline2",
   "security/path": "/var/log/systems.log"
}

I would like to filter by key security/path, and below commands all don't work.

jq .security/path test.json
jq: error: path/0 is not defined at <top-level>, line 1:
.security/path
jq: 1 compile error

jq '.security/path' test.json has same result.

fhcat
  • 231

2 Answers2

22

From Basic filters:

Object Identifier-Index:

...

If the key contains special characters or starts with a digit, you need to surround it with double quotes like this: ."foo$", or else .["foo$"]

....

So,

$ jq -r '."security/path"' test.json
/var/log/systems.log
Kusalananda
  • 333,661
steeldriver
  • 81,074
1

In case your output is something like this,

  "annotations": {
    "eks.amazonaws.com/role-arn": "arn:aws:iam::907636088649:role/stg-eks_helm-chart",
    "meta.helm.sh/release-name": "helm-chart-base",
    "meta.helm.sh/release-namespace": "stg-helm-chart"
  }

Following will work,

jq -r '.metadata.annotations["eks.amazonaws.com/role-arn"]'

and will produce an output like this,

arn:aws:iam::907636088649:role/stg-eks_helm-chart
redzack
  • 119
  • You would not use tr to remove the double quotes, you would use jq with its -r option. That way you get the properly decoded string from jq (which would make a difference, for example, to the cert string in the question, which contains an encoded newline). Note too that your answer seems to be identical to the older answer from steeldriver, and that you're using broken JSON which jq will not parse as an example. – Kusalananda Oct 18 '22 at 17:59
  • kinda identical yes, just posted what I was trying to do, -r yea missed that, and updated the answer. Thanks. – redzack Oct 18 '22 at 19:32