我有以下json文件:

{
    "FOO": {
        "name": "Donald",
        "location": "Stockholm"
    },
    "BAR": {
        "name": "Walt",
        "location": "Stockholm"
    },
    "BAZ": {
        "name": "Jack",
        "location": "Whereever"
    }
}

我正在使用jq,想要获得对象的“名称”元素,其中“位置”是“斯德哥尔摩”。

我知道我可以通过

cat json | jq .[] | jq ."name"
"Jack"
"Walt"
"Donald"

但是我不知道如何只打印特定的对象,给定一个子键的值(这里:"location": "Stockholm")。


当前回答

只是尝试这一个作为一个完整的复制粘贴在外壳,你会抓住它。

# pass the multiline string to the jq, use the jq to 
# select the attribute named "card_id" 
# ONLY if its neighbour attribute
# named "card_id_type" has the "card_id_type-01" value.
# jq -r means give me ONLY the value of the jq query no quotes aka raw


cat << EOF | \
    jq -r '.[]| select (.card_id_type == "card_id_type-01")|.card_id'
    [  
     { "card_id": "id-00", "card_id_type": "card_id_type-00"},
     { "card_id": "id-01", "card_id_type": "card_id_type-01"},
     { "card_id": "id-02", "card_id_type": "card_id_type-02"}
    ]
EOF
# this ^^^ MUST start first on the line - no whitespace there !!!
# outputs:
# id-01

或者使用aws cli命令

 # list my vpcs or
 # list the values of the tags which names are "Name" 
 aws ec2 describe-vpcs | jq -r '.| .Vpcs[].Tags[]
        |select (.Key == "Name") | .Value'|sort  -nr

注意,你可以在过滤阶段和选择阶段在层次结构中上下移动:

 kubectl get services --all-namespaces -o json | jq -r '
 .items[] | select( .metadata.name 
     | contains("my-srch-string")) | 
     { name: .metadata.name, ns: .metadata.namespace 
     , nodePort: .spec.ports[].nodePort
     , port: .spec.ports[].port}
 '

其他回答

改编自这篇关于用jq处理JSON的文章,你可以像这样使用select(bool):

$ jq '.[] | select(.location=="Stockholm")' json
{
  "location": "Stockholm",
  "name": "Walt"
}
{
  "location": "Stockholm",
  "name": "Donald"
}

我有一个类似的相关问题:如果你想要回原来的对象格式(带键名,例如FOO, BAR)怎么办?

Jq提供了to_entries和from_entries来在对象和键值对数组之间进行转换。它和select周围的map一起

这些函数在对象和键值数组之间转换 对。如果传递给to_entries一个对象,则对于每个k: v条目 输入,输出数组包括{"key": k, "value": v}。 From_entries做相反的转换,with_entries(foo)是一个 to_entries | map(foo) | from_entries的简写,用于做 对对象的所有键和值进行一些操作。from_entries 接受键,键,名称,名称,值和值作为键。

jq15 < json 'to_entries | map(select(.value.location=="Stockholm")) | from_entries'

{
  "FOO": {
    "name": "Donald",
    "location": "Stockholm"
  },
  "BAR": {
    "name": "Walt",
    "location": "Stockholm"
  }
}

使用with_entries简写,它变成:

jq15 < json 'with_entries(select(.value.location=="Stockholm"))'
{
  "FOO": {
    "name": "Donald",
    "location": "Stockholm"
  },
  "BAR": {
    "name": "Walt",
    "location": "Stockholm"
  }
}

只是尝试这一个作为一个完整的复制粘贴在外壳,你会抓住它。

# pass the multiline string to the jq, use the jq to 
# select the attribute named "card_id" 
# ONLY if its neighbour attribute
# named "card_id_type" has the "card_id_type-01" value.
# jq -r means give me ONLY the value of the jq query no quotes aka raw


cat << EOF | \
    jq -r '.[]| select (.card_id_type == "card_id_type-01")|.card_id'
    [  
     { "card_id": "id-00", "card_id_type": "card_id_type-00"},
     { "card_id": "id-01", "card_id_type": "card_id_type-01"},
     { "card_id": "id-02", "card_id_type": "card_id_type-02"}
    ]
EOF
# this ^^^ MUST start first on the line - no whitespace there !!!
# outputs:
# id-01

或者使用aws cli命令

 # list my vpcs or
 # list the values of the tags which names are "Name" 
 aws ec2 describe-vpcs | jq -r '.| .Vpcs[].Tags[]
        |select (.Key == "Name") | .Value'|sort  -nr

注意,你可以在过滤阶段和选择阶段在层次结构中上下移动:

 kubectl get services --all-namespaces -o json | jq -r '
 .items[] | select( .metadata.name 
     | contains("my-srch-string")) | 
     { name: .metadata.name, ns: .metadata.namespace 
     , nodePort: .spec.ports[].nodePort
     , port: .spec.ports[].port}
 '

获取一个只包含名称的流:

$ jq '.[] | select(.location=="Stockholm") | .name' json

生产:

"Donald"
"Walt"

要获得相应的(键名,"name"属性)对流,考虑:

$ jq -c 'to_entries[]
        | select (.value.location == "Stockholm")
        | [.key, .value.name]' json

输出:

["FOO","Donald"]
["BAR","Walt"]