【问题标题】:How to get the keyid of an object in a JSON file if one of the fields contains a specific string如果其中一个字段包含特定字符串,如何获取 JSON 文件中对象的 keyid
【发布时间】:2018-09-09 12:21:44
【问题描述】:

我有以下 JSON:

{
  "201110131738QP27N": {
    "parent": 17,
    "name": "CentralServer",
    "status": "Active",
    "count": 6
  },
  "201803271459ICV69": {
    "name": "subaccount1",
    "status": "Active",
    "count": 1
  },
  "2018032715008ZM2G": {
    "name": "subaccount2",
    "status": "Active",
    "count": 1
  },
  "201803281536PSKR4": {
    "name": "Este e um teste",
    "status": "Active"
  }
}

我想知道是否有任何方法可以输入密钥名称的内容并且“jq”给我 KeyID? 例如: 我在命令上通知“subaccount1”,jq 返回我:201803271459ICV69

我正在尝试以下命令,但没有成功。

cat file.json | jq -r '. | select(.[].name | contains("subaccount2")) | keys '

【问题讨论】:

    标签: json bash select filter jq


    【解决方案1】:

    jq解决方案:

    jq -r 'to_entries[] | select(.value.name == "subaccount1").key' file.json
    

    输出:

    201803271459ICV69
    

    【讨论】:

    • 太棒了!完美的工作非常感谢你
    猜你喜欢
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-02
    相关资源
    最近更新 更多