考虑一下这个,从this questionjson

{
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
        "annotations": {
            "deployment.kubernetes.io/revision": "1"
        },
        "creationTimestamp": "2023-02-22T17:35:22Z",
        "generation": 1,
        "labels": {
            "app": "addresses",
            "group": "de.test",
            "provider": "jkube",
            "version": "2.3.0-3191"
        },
        "name": "addresses",
        "namespace": "linkerd",
        "resourceVersion": "910972",
        "uid": "aaee0eb0-5d66-44dd-b9b0-35e17bec4cf4"
    },
    "spec": {
        "progressDeadlineSeconds": 600,
        "replicas": 1,
        "revisionHistoryLimit": 2,
        "selector": {
            "matchLabels": {
                "app": "addresses",
                "group": "de.test",
                "provider": "jkube"
            }
        },
        "strategy": {
            "rollingUpdate": {
                "maxSurge": 1,
                "maxUnavailable": "50%"
            },
            "type": "RollingUpdate"
        },
        "template": {
            "metadata": {
                "annotations": {
                    "jkube.io/git-branch": "develop",
                    "jkube.io/git-commit": "77aaca38564dffff0ce10a8c70d4139b33f677b2",
                    "jkube.io/git-url": "http://bitbucket.org/testzentrale/test-monorepo",
                    "jkube.io/scm-tag": "HEAD",
                    "jkube.io/scm-url": "https://bitbucket.org/testzentrale/test-monorepo/test/addresses"
                },
                "creationTimestamp": null,
                "labels": {
                    "app": "addresses",
                    "group": "de.test",
                    "provider": "jkube",
                    "version": "2.3.0-3191"
                },
                "namespace": "test-testing"
            },
            "spec": {
                "containers": [
                    {
                        "env": [
                            {
                                "name": "SENTRY_ENVIRONMENT",
                                "valueFrom": {
                                    "fieldRef": {
                                        "apiVersion": "v1",
                                        "fieldPath": "metadata.namespace"
                                    }
                                }
                            },
                            {
                                "name": "SENTRY_SERVERNAME",
                                "valueFrom": {
                                    "fieldRef": {
                                        "apiVersion": "v1",
                                        "fieldPath": "metadata.name"
                                    }
                                }
                            },
                            {
                                "name": "SENTRY_TAGS",
                                "value": "service:addresses"
                            },
                            {
                                "name": "SENTRY_RELEASE",
                                "value": "2.3.0-3191"
                            },
                            {
                                "name": "JAVA_OPTIONS",
                                "value": "-Xms64m -Xmx384m"
                            },
                            {
                                "name": "KUBERNETES_NAMESPACE",
                                "valueFrom": {
                                    "fieldRef": {
                                        "apiVersion": "v1",
                                        "fieldPath": "metadata.namespace"
                                    }
                                }
                            },
                            {
                                "name": "HOSTNAME",
                                "valueFrom": {
                                    "fieldRef": {
                                        "apiVersion": "v1",
                                        "fieldPath": "metadata.name"
                                    }
                                }
                            }
                        ],
                        "envFrom": [
                            {
                                "configMapRef": {
                                    "name": "db-mssql-asd-connection"
                                }
                            },
                            {
                                "secretRef": {
                                    "name": "db-mssql-asd-connection"
                                }
                            },
                            {
                                "configMapRef": {
                                    "name": "db-postgre-asd-connection"
                                }
                            },
                            {
                                "secretRef": {
                                    "name": "db-postgre-asd-connection"
                                }
                            },
                            {
                                "configMapRef": {
                                    "name": "app-url"
                                }
                            },
                            {
                                "secretRef": {
                                    "name": "app-token"
                                }
                            }                                                      
                        ],
                        "image": "myrepo/test/addresses:testing-2.3.0-3191",
                        "imagePullPolicy": "IfNotPresent",
                        "livenessProbe": {
                            "failureThreshold": 3,
                            "httpGet": {
                                "path": "/",
                                "port": 8080,
                                "scheme": "HTTP"
                            },
                            "initialDelaySeconds": 300,
                            "periodSeconds": 10,
                            "successThreshold": 1,
                            "timeoutSeconds": 1
                        },
                        "name": "spring-boot",
                        "ports": [
                            {
                                "containerPort": 8080,
                                "name": "http",
                                "protocol": "TCP"
                            },
                            {
                                "containerPort": 9779,
                                "name": "prometheus",
                                "protocol": "TCP"
                            },
                            {
                                "containerPort": 8778,
                                "name": "jolokia",
                                "protocol": "TCP"
                            }
                        ],
                        "readinessProbe": {
                            "failureThreshold": 2,
                            "httpGet": {
                                "path": "/actuator/health",
                                "port": 8080,
                                "scheme": "HTTP"
                            },
                            "initialDelaySeconds": 20,
                            "periodSeconds": 10,
                            "successThreshold": 1,
                            "timeoutSeconds": 1
                        },
                        "resources": {
                            "limits": {
                                "memory": "1000Mi"
                            },
                            "requests": {
                                "cpu": "100m",
                                "memory": "500Mi"
                            }
                        },
                        "securityContext": {
                            "privileged": false
                        },
                        "terminationMessagePath": "/dev/termination-log",
                        "terminationMessagePolicy": "File",
                        "volumeMounts": [
                            {
                                "mountPath": "/deployments/config",
                                "name": "config"
                            }
                        ]
                    }
                ],
                "dnsPolicy": "ClusterFirst",
                "restartPolicy": "Always",
                "schedulerName": "default-scheduler",
                "securityContext": {},
                "terminationGracePeriodSeconds": 30,
                "volumes": [
                    {
                        "configMap": {
                            "defaultMode": 420,
                            "items": [
                                {
                                    "key": "application.properties",
                                    "path": "application.properties"
                                }
                            ],
                            "name": "addresses"
                        },
                        "name": "config"
                    }
                ]
            }
        }
    },
    "status": {
        "conditions": [
            {
                "lastTransitionTime": "2023-02-22T17:35:23Z",
                "lastUpdateTime": "2023-02-22T17:35:23Z",
                "message": "Deployment does not have minimum availability.",
                "reason": "MinimumReplicasUnavailable",
                "status": "False",
                "type": "Available"
            },
            {
                "lastTransitionTime": "2023-02-22T17:45:24Z",
                "lastUpdateTime": "2023-02-22T17:45:24Z",
                "message": "ReplicaSet \"addresses-dd575cdc9\" has timed out progressing.",
                "reason": "ProgressDeadlineExceeded",
                "status": "False",
                "type": "Progressing"
            }
        ],
        "observedGeneration": 1,
        "replicas": 1,
        "unavailableReplicas": 1,
        "updatedReplicas": 1
    }
}

你可以通过this JSON gist下载

curl -s https://gist.githubusercontent.com/sputnick-dev/7da057b951c182785dfb837194388350/raw/eaf064c6b44c8304e111fa4e6ee3638d47a4ef48/object.json > object.json

有没有办法获取/提取configMapRef的所有名称属性?

(搜索了here个标签[xidel][json],只有两个匹配).

Expected output:

db-mssql-asd-connection
db-postgre-asd-connection
app-url

这是一个简单的例子,其中我得到xidel来解析json,并带有简短的样本:

$ cat object.json 
{
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
        "annotations": {
            "deployment.kubernetes.io/revision": "1"
        }
    }
}
$ xidel -e '($json).metadata.annotations["deployment.kubernetes.io/revision"]' -s object.json 
{
  "deployment.kubernetes.io/revision": "1"
}

推荐答案

Is there a way to fetch/extract all the name properties of the 100 ?

当然有:

$ xidel -s --input-format=json "https://gist.githubusercontent.com/sputnick-dev/7da057b951c182785dfb837194388350/raw/eaf064c6b44c8304e111fa4e6ee3638d47a4ef48/object.json" \
  -e '($json).spec.template.spec.containers().envFrom().configMapRef.name'     # dot notation
  -e '$json/spec/template/spec/(containers)()/(envFrom)()/configMapRef/name'   # XPath-like notation
  -e '$json?spec?template?spec?containers?*?envFrom?*?configMapRef?name'       # XPath 3.1 syntax

(Github gist-url is JSON,但$headers返回Content-Type: text/plain; charset=utf-8,而不是Content-Type: application/json,所以用--input-format=json来强制它.)

您可以将类似XPath的表示法缩短为:

-e '($json//envFrom)()/configMapRef/name'

Json相关问答推荐

服务器不返回JSON

如何使用表键名称GROUP_BY

时间序列的Vega Lite分组条形图

使用JQ将对象数组转换为平面数组

如何在Android中解析带有动态键和可变对象名称的改装JSON响应?

用巨大的值更新SQL Server中的nvarchar(max)

如何在 jq 中按 IP 地址排序?

如何在 Apps 脚本中循环遍历 JSON 响应

如何 Select 一个值,这是可选的 - 使用 jq

使用 jq 将消息转换为数组

Powershell 无法从名为 count 的键中获取价值

使用 Jolt 使用键中的值创建带有硬编码键和值的 JSON 数组

Google GCM 服务器返回未经授权的错误 401

Java的JSON字符串整洁/格式化程序

读取 HttpwebResponse json 响应,C#

使用 API 搜索维基百科

Jackson JSON序列化,通过级别定义避免递归

JSON 模式 - 如果对象*不*包含特定属性则有效

Protocol Buffer vs Json - 何时 Select 一个而不是另一个

Volley JsonObjectRequest Post 参数不再起作用