bash 中的 json 过滤

发布于 2025-01-10 02:01:39 字数 1665 浏览 1 评论 0原文

我想问你是否可以帮我过滤json数据。我有以下 json:

{
    "cluster_name": "k8s-cluster-1",
    "cluster_env": "PROD",
    "cluster_hosts": [
      {
        "host_type": "MASTER",
        "host_hostname": "aa083",
        "host_username": "kubeadm",
        "host_kubernetes_version": "",
      },
      {
        "host_type": "NODE",
        "host_hostname": "aa084",
        "host_username": "kubeadm",
        "host_kubernetes_version": "",
      },
      {
        "host_type": "NODE",
        "host_hostname": "aa085",
        "host_username": "kubeadm",
        "host_kubernetes_version": "",
      }
    ],
},
{
    "cluster_name": "k8s-cluster-2",
    "cluster_env": "PROD",
    "cluster_hosts": [
      {
        "host_type": "MASTER",
        "host_hostname": "ab093",
        "host_username": "kubeadm",
        "host_kubernetes_version": "",
      },
      {
        "host_type": "NODE",
        "host_hostname": "ab094",
        "host_username": "kubeadm",
        "host_kubernetes_version": "",
      },
      {
        "host_type": "NODE",
        "host_hostname": "ab095",
        "host_username": "kubeadm",
        "host_kubernetes_version": "",
      }
    ],
}

输出应如下所示:

cluster_name
host_type
host_name
.
.
.
k8s-cluster-1
MASTER
aa083
NODE
aa084
NODE
aa085
k8s-cluster-2
MASTER
ab093
NODE
ab094
NODE
ab095

我尝试了此命令

cat json |jq -c '.[] | .cluster_name,.cluster_hosts[] | .cluster_name,.cluster_hosts[] | {host_type,host_name}'

但它显示错误

jq: error (at :1): Cannot index string with string "host_type"

我需要在 bash 中执行此操作。

请你帮助我好吗?

先感谢您。

I would like to ask you if you can help me with filtering data in json. I have the following json:

{
    "cluster_name": "k8s-cluster-1",
    "cluster_env": "PROD",
    "cluster_hosts": [
      {
        "host_type": "MASTER",
        "host_hostname": "aa083",
        "host_username": "kubeadm",
        "host_kubernetes_version": "",
      },
      {
        "host_type": "NODE",
        "host_hostname": "aa084",
        "host_username": "kubeadm",
        "host_kubernetes_version": "",
      },
      {
        "host_type": "NODE",
        "host_hostname": "aa085",
        "host_username": "kubeadm",
        "host_kubernetes_version": "",
      }
    ],
},
{
    "cluster_name": "k8s-cluster-2",
    "cluster_env": "PROD",
    "cluster_hosts": [
      {
        "host_type": "MASTER",
        "host_hostname": "ab093",
        "host_username": "kubeadm",
        "host_kubernetes_version": "",
      },
      {
        "host_type": "NODE",
        "host_hostname": "ab094",
        "host_username": "kubeadm",
        "host_kubernetes_version": "",
      },
      {
        "host_type": "NODE",
        "host_hostname": "ab095",
        "host_username": "kubeadm",
        "host_kubernetes_version": "",
      }
    ],
}

And the output should look like this:

cluster_name
host_type
host_name
.
.
.
k8s-cluster-1
MASTER
aa083
NODE
aa084
NODE
aa085
k8s-cluster-2
MASTER
ab093
NODE
ab094
NODE
ab095

I tried this command

cat json |jq -c '.[] | .cluster_name,.cluster_hosts[] | {host_type,host_name}'

but it shows an error

jq: error (at :1): Cannot index string with string "host_type"

I need to do this in bash.

Could you please help me?

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨敛清荷 2025-01-17 02:01:39

您的实际输入文件有几个多余的逗号。假设您的 JSON 文件应该是对象流,即如下所示:

{
    "cluster_name": "k8s-cluster-1",
    "cluster_env": "PROD",
    "cluster_hosts": [
      {
        "host_type": "MASTER",
        "host_hostname": "aa083",
        "host_username": "kubeadm",
        "host_kubernetes_version": ""
      },
      {
        "host_type": "NODE",
        "host_hostname": "aa084",
        "host_username": "kubeadm",
        "host_kubernetes_version": ""
      },
      {
        "host_type": "NODE",
        "host_hostname": "aa085",
        "host_username": "kubeadm",
        "host_kubernetes_version": ""
      }
    ]
}
{
    "cluster_name": "k8s-cluster-2",
    "cluster_env": "PROD",
    "cluster_hosts": [
      {
        "host_type": "MASTER",
        "host_hostname": "ab093",
        "host_username": "kubeadm",
        "host_kubernetes_version": ""
      },
      {
        "host_type": "NODE",
        "host_hostname": "ab094",
        "host_username": "kubeadm",
        "host_kubernetes_version": ""
      },
      {
        "host_type": "NODE",
        "host_hostname": "ab095",
        "host_username": "kubeadm",
        "host_kubernetes_version": ""
      }
    ]
}

那么您只需列出您感兴趣的字段。使用 -r 标志输出原始文本而不是 JSON 字符串(将被引用):

jq -r '.cluster_name, (.cluster_hosts[] | .host_type, .host_hostname)'
k8s-cluster-1
MASTER
aa083
NODE
aa084
NODE
aa085
k8s-cluster-2
MASTER
ab093
NODE
ab094
NODE
ab095

Demo

如果您的输入文件应该是一个数组(在这种情况下顶层逗号是正确的,但缺少数组括号 []),然后只需在过滤器前面添加 .[] | 即可。

Your actual input file has several superfluous commas. Assuming your JSON file should be a stream of objects, i.e. look like this:

{
    "cluster_name": "k8s-cluster-1",
    "cluster_env": "PROD",
    "cluster_hosts": [
      {
        "host_type": "MASTER",
        "host_hostname": "aa083",
        "host_username": "kubeadm",
        "host_kubernetes_version": ""
      },
      {
        "host_type": "NODE",
        "host_hostname": "aa084",
        "host_username": "kubeadm",
        "host_kubernetes_version": ""
      },
      {
        "host_type": "NODE",
        "host_hostname": "aa085",
        "host_username": "kubeadm",
        "host_kubernetes_version": ""
      }
    ]
}
{
    "cluster_name": "k8s-cluster-2",
    "cluster_env": "PROD",
    "cluster_hosts": [
      {
        "host_type": "MASTER",
        "host_hostname": "ab093",
        "host_username": "kubeadm",
        "host_kubernetes_version": ""
      },
      {
        "host_type": "NODE",
        "host_hostname": "ab094",
        "host_username": "kubeadm",
        "host_kubernetes_version": ""
      },
      {
        "host_type": "NODE",
        "host_hostname": "ab095",
        "host_username": "kubeadm",
        "host_kubernetes_version": ""
      }
    ]
}

Then you just need to list the fields you are interested in. Use the -r flag to output raw text instead of JSON strings (which would be quoted):

jq -r '.cluster_name, (.cluster_hosts[] | .host_type, .host_hostname)'
k8s-cluster-1
MASTER
aa083
NODE
aa084
NODE
aa085
k8s-cluster-2
MASTER
ab093
NODE
ab094
NODE
ab095

Demo

If instead your input file is supposed to be an array (in which case the top-level commas were correct but the array brackets [] were missing), then simply prepend the filter with .[] |.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文