bash 中的 json 过滤
我想问你是否可以帮我过滤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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的实际输入文件有几个多余的逗号。假设您的 JSON 文件应该是对象流,即如下所示:
那么您只需列出您感兴趣的字段。使用
-r
标志输出原始文本而不是 JSON 字符串(将被引用):Demo
如果您的输入文件应该是一个数组(在这种情况下顶层逗号是正确的,但缺少数组括号
[]
),然后只需在过滤器前面添加.[] |
即可。Your actual input file has several superfluous commas. Assuming your JSON file should be a stream of objects, i.e. look like this:
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):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.[] |
.