使用 jq 根据哈希中的键选择对象

发布于 2025-01-11 06:57:58 字数 1211 浏览 0 评论 0原文

我很想根据哈希中的键选择对象,但我希望恢复原始对象格式。我怎样才能实现这个目标?

原始

{
  "google.com": {
    "http": {
      "dest_url": "http://stackoverflow.com"
    },
    "https": {
      "dest_url": "http://github.com"
    }
  },
  "aaa.com": {
    "https": {
      "dest_url": "https://github.com"
    }
  },
  "bbb.com": {
    "https": {
      "dest_url": "https://microsoft.com"
    }
  },
   "ccc.com": {
    "https": {
      "dest_url": "https://.com"
    }
  }
}

应该是

{
  "google.com": {
    "http": {
      "dest_url": "http://stackoverflow.com"
    },
    "https": {
      "dest_url": "http://github.com"
    }
  },
  "bbb.com": {
    "https": {
      "dest_url": "https://microsoft.com"
    }
  }
}

我用 to_entries[] | 尝试过它选择 (.key == "google.com" 或 .key == "bbb.com") | [.key, .value] 但它是这样的。我确信 [.key, .value] 部分是错误的,但我一直不知道如何解决这个问题。

[
  "google.com",
  {
    "http": {
      "dest_url": "http://stackoverflow.com"
    },
    "https": {
      "dest_url": "http://github.com"
    }
  }
]
[
  "bbb.com",
  {
    "https": {
      "dest_url": "https://microsoft.com"
    }
  }
]

I'd love to select objects based on the key in the hash, but I want the original object format back. How can I achieve this?

original

{
  "google.com": {
    "http": {
      "dest_url": "http://stackoverflow.com"
    },
    "https": {
      "dest_url": "http://github.com"
    }
  },
  "aaa.com": {
    "https": {
      "dest_url": "https://github.com"
    }
  },
  "bbb.com": {
    "https": {
      "dest_url": "https://microsoft.com"
    }
  },
   "ccc.com": {
    "https": {
      "dest_url": "https://.com"
    }
  }
}

should be

{
  "google.com": {
    "http": {
      "dest_url": "http://stackoverflow.com"
    },
    "https": {
      "dest_url": "http://github.com"
    }
  },
  "bbb.com": {
    "https": {
      "dest_url": "https://microsoft.com"
    }
  }
}

I tried it with to_entries[] | select (.key == "google.com" or .key == "bbb.com") | [.key, .value] but it eneded like this. I'm sure [.key, .value] part is wrong but I'm stuck on how I can workaround this.

[
  "google.com",
  {
    "http": {
      "dest_url": "http://stackoverflow.com"
    },
    "https": {
      "dest_url": "http://github.com"
    }
  }
]
[
  "bbb.com",
  {
    "https": {
      "dest_url": "https://microsoft.com"
    }
  }
]

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

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

发布评论

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

评论(1

明月夜 2025-01-18 06:57:58

使用 with_entries(…),这是 to_entries | 的快捷方式。地图(…)| from_entries,它又将对象分解为键/值表示数组 to_entries,修改该数组 map 中的每个项目,并将其转换回原始结构from_entries

jq 'with_entries(select(.key == "google.com" or .key == "bbb.com"))'
{
  "google.com": {
    "http": {
      "dest_url": "http://stackoverflow.com"
    },
    "https": {
      "dest_url": "http://github.com"
    }
  },
  "bbb.com": {
    "https": {
      "dest_url": "https://microsoft.com"
    }
  }
}

演示

Use with_entries(…), which is a shortcut to to_entries | map(…) | from_entries, which in turn decomposes the object into an array of key/value representations to_entries, modifies each item within that array map, and converts it back into the original structure from_entries.

jq 'with_entries(select(.key == "google.com" or .key == "bbb.com"))'
{
  "google.com": {
    "http": {
      "dest_url": "http://stackoverflow.com"
    },
    "https": {
      "dest_url": "http://github.com"
    }
  },
  "bbb.com": {
    "https": {
      "dest_url": "https://microsoft.com"
    }
  }
}

Demo

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