gpt4 book ai didi

json - 如何使用 gcloud 命令从 json 中仅提取名称字段?

转载 作者:行者123 更新时间:2023-12-03 08:35:44 26 4
gpt4 key购买 nike

我是 gcp 和 linux 的新手。我正在尝试从 json 获取名称使用gcloud格式命令。以下是命令和输出:

gcloud firestore indexes composite list --format=json

输出:

[
{
"fields": [
{
"fieldPath": "is_active",
"order": "ASCENDING"
},
{
"fieldPath": "created_at",
"order": "DESCENDING"
},
{
"fieldPath": "__name__",
"order": "ASCENDING"
}
],
"name": "projects/emc-ema-simcs-d-286404/databases/(default)/collectionGroups/customer/indexes/CICAgNjbhowK",
"queryScope": "COLLECTION",
"state": "READY"
}

如何获得name使用 gcloud 命令的值。我尝试使用:gcloud firestore indexes composite list | grep name | awk -F'name: ' '{print $2}'但这没有给出任何输出。

请帮忙!

谢谢普里蒂什

最佳答案

假设您的工具输出确实没有无与伦比的 [在开始时,这会使其无效 JSON,如您在问题中提供的示例所示,而是同时具有 []或者两者都没有,即它是有效的 JSON。

如果输出以 [ 开头并以 ] 结尾那么你会使用jq如:

gcloud firestore indexes composite list --format=json | jq -r '.[0] .name'

否则你会使用 jq如:

gcloud firestore indexes composite list --format=json | jq -r '.name'

例如,使用 cat file<N>代替gcloud firestore indexes composite list --format=json我没有:

$ cat file1
[
{
"fields": [
{
"fieldPath": "is_active",
"order": "ASCENDING"
},
{
"fieldPath": "created_at",
"order": "DESCENDING"
},
{
"fieldPath": "__name__",
"order": "ASCENDING"
}
],
"name": "projects/emc-ema-simcs-d-286404/databases/(default)/collectionGroups/customer/indexes/CICAgNjbhowK",
"queryScope": "COLLECTION",
"state": "READY"
}
]
$
$ cat file1 | jq -r '.[0] .name'
projects/emc-ema-simcs-d-286404/databases/(default)/collectionGroups/customer/indexes/CICAgNjbhowK

$ cat file2
{
"fields": [
{
"fieldPath": "is_active",
"order": "ASCENDING"
},
{
"fieldPath": "created_at",
"order": "DESCENDING"
},
{
"fieldPath": "__name__",
"order": "ASCENDING"
}
],
"name": "projects/emc-ema-simcs-d-286404/databases/(default)/collectionGroups/customer/indexes/CICAgNjbhowK",
"queryScope": "COLLECTION",
"state": "READY"
}
$
$ cat file2 | jq -r '.name'
projects/emc-ema-simcs-d-286404/databases/(default)/collectionGroups/customer/indexes/CICAgNjbhowK

关于json - 如何使用 gcloud 命令从 json 中仅提取名称字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63921338/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com