使用某些关键字从JQ构建JSON路径

时间:2019-03-28 21:06:20

标签: json jq

我有一个很深的json。有时,我需要寻找包含某些单词的键的json路径。

{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        "creationTimestamp": "2019-03-28T21:09:42Z",
        "labels": {
            "bu": "finance",
            "env": "prod"
        },
        "name": "auth",
        "namespace": "default",
        "resourceVersion": "2786",
        "selfLink": "/api/v1/namespaces/default/pods/auth",
        "uid": "ce73565a-519d-11e9-bcb7-0242ac110009"
    },
    "spec": {
        "containers": [
            {
                "command": [
                    "sleep",
                    "4800"
                ],
                "image": "busybox",
                "imagePullPolicy": "Always",
                "name": "busybox",
                "resources": {},
                "terminationMessagePath": "/dev/termination-log",
                "terminationMessagePolicy": "File",
                "volumeMounts": [
                    {
                        "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
                        "name": "default-token-dbpcm",
                        "readOnly": true
                    }
                ]
            }
        ],
        "dnsPolicy": "ClusterFirst",
        "nodeName": "node01",
        "priority": 0,
        "restartPolicy": "Always",
        "schedulerName": "default-scheduler",
        "securityContext": {},
        "serviceAccount": "default",
        "serviceAccountName": "default",
        "terminationGracePeriodSeconds": 30,
        "tolerations": [
            {
                "effect": "NoExecute",
                "key": "node.kubernetes.io/not-ready",
                "operator": "Exists",
                "tolerationSeconds": 300
            },
            {
                "effect": "NoExecute",
                "key": "node.kubernetes.io/unreachable",
                "operator": "Exists",
                "tolerationSeconds": 300
            }
        ],
        "volumes": [
            {
                "name": "default-token-dbpcm",
                "secret": {
                    "defaultMode": 420,
                    "secretName": "default-token-dbpcm"
                }
            }
        ]
    },
    "status": {
        "conditions": [
            {
                "lastProbeTime": null,
                "lastTransitionTime": "2019-03-28T21:09:42Z",
                "status": "True",
                "type": "Initialized"
            },
            {
                "lastProbeTime": null,
                "lastTransitionTime": "2019-03-28T21:09:50Z",
                "status": "True",
                "type": "Ready"
            },
            {
                "lastProbeTime": null,
                "lastTransitionTime": null,
                "status": "True",
                "type": "ContainersReady"
            },
            {
                "lastProbeTime": null,
                "lastTransitionTime": "2019-03-28T21:09:42Z",
                "status": "True",
                "type": "PodScheduled"
            }
        ],
        "containerStatuses": [
            {
                "containerID": "docker://b5be8275555ad70939401d658bb4e504b52215b70618ad43c2d0d02c35e1ae27",
                "image": "busybox:latest",
                "imageID": "docker-pullable://busybox@sha256:061ca9704a714ee3e8b80523ec720c64f6209ad3f97c0ff7cb9ec7d19f15149f",
                "lastState": {},
                "name": "busybox",
                "ready": true,
                "restartCount": 0,
                "state": {
                    "running": {
                        "startedAt": "2019-03-28T21:09:49Z"
                    }
                }
            }
        ],
        "hostIP": "172.17.0.37",
        "phase": "Running",
        "podIP": "10.32.0.4",
        "qosClass": "BestEffort",
        "startTime": "2019-03-28T21:09:42Z"
    }
}

当前,如果我需要podIP,则可以通过这种方式找到具有搜索关键字的对象,然后构建路径

curl myson | jq "[paths]" | grep "IP" --context=10

是否有任何简便的快捷方式可以简化此操作?我真正需要的是-可能具有匹配键的所有路径。

spec.podIP
spec.hostIP

3 个答案:

答案 0 :(得分:1)

select paths的最后一个元素中包含keyword,并使用join(".")生成所需的输出。

paths
| select(.[-1] | type == "string" and contains("keyword"))
| join(".")
  • .[-1]返回数组的最后一个元素,
  • type == "string"是必需的,因为数组索引是一个数字,并且无法检查数字和字符串是否包含它们。

您可能要指定-r选项。


正如@JeffMercado隐式建议的那样,您可以从命令行设置查询而无需触摸脚本:

jq -r 'paths
| select(.[-1] | type == "string" and contains($q))
| join(".")' file.json --arg q 'keyword'

答案 1 :(得分:0)

您可以输入流,其中提供了路径和值。然后,您可以检查路径并有选择地输出值。

$ jq --stream --arg pattern 'IP' '
select(length == 2 and any(.[0][] | strings; test($pattern)))
    | "\(.[0] | join(".")): \(.[1])"
' input.json
"status.hostIP: 172.17.0.37"
"status.podIP: 10.32.0.4"

答案 2 :(得分:0)

无耻的插头

https://github.com/TomConlin/json_to_paths

因为有时在看到组件之前,您甚至不知道要过滤的组件。

json2jqpath.jq file.json 
.
.apiVersion
.kind
.metadata
.metadata|.creationTimestamp
.metadata|.labels
.metadata|.labels|.bu
.metadata|.labels|.env
.metadata|.name
.metadata|.namespace
.metadata|.resourceVersion
.metadata|.selfLink
.metadata|.uid
.spec
.spec|.containers
.spec|.containers|.[]
.spec|.containers|.[]|.command
.spec|.containers|.[]|.command|.[]
.spec|.containers|.[]|.image
.spec|.containers|.[]|.imagePullPolicy
.spec|.containers|.[]|.name
.spec|.containers|.[]|.resources
.spec|.containers|.[]|.terminationMessagePath
.spec|.containers|.[]|.terminationMessagePolicy
.spec|.containers|.[]|.volumeMounts
.spec|.containers|.[]|.volumeMounts|.[]
.spec|.containers|.[]|.volumeMounts|.[]|.mountPath
.spec|.containers|.[]|.volumeMounts|.[]|.name
.spec|.containers|.[]|.volumeMounts|.[]|.readOnly
.spec|.dnsPolicy
.spec|.nodeName
.spec|.priority
.spec|.restartPolicy
.spec|.schedulerName
.spec|.securityContext
.spec|.serviceAccount
.spec|.serviceAccountName
.spec|.terminationGracePeriodSeconds
.spec|.tolerations
.spec|.tolerations|.[]
.spec|.tolerations|.[]|.effect
.spec|.tolerations|.[]|.key
.spec|.tolerations|.[]|.operator
.spec|.tolerations|.[]|.tolerationSeconds
.spec|.volumes
.spec|.volumes|.[]
.spec|.volumes|.[]|.name
.spec|.volumes|.[]|.secret
.spec|.volumes|.[]|.secret|.defaultMode
.spec|.volumes|.[]|.secret|.secretName
.status
.status|.conditions
.status|.conditions|.[]
.status|.conditions|.[]|.lastProbeTime
.status|.conditions|.[]|.lastTransitionTime
.status|.conditions|.[]|.status
.status|.conditions|.[]|.type
.status|.containerStatuses
.status|.containerStatuses|.[]
.status|.containerStatuses|.[]|.containerID
.status|.containerStatuses|.[]|.image
.status|.containerStatuses|.[]|.imageID
.status|.containerStatuses|.[]|.lastState
.status|.containerStatuses|.[]|.name
.status|.containerStatuses|.[]|.ready
.status|.containerStatuses|.[]|.restartCount
.status|.containerStatuses|.[]|.state
.status|.containerStatuses|.[]|.state|.running
.status|.containerStatuses|.[]|.state|.running|.startedAt
.status|.hostIP
.status|.phase
.status|.podIP
.status|.qosClass
.status|.startTime

相关问题