从 JSON 中查找 key 的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39651946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:38:27  来源:igfitidea点击:

Find the value of key from JSON

jsongrep

提问by petebocken

I'd like to extract the "id"key from this single line of JSON.

我想"id"从这一行 JSON 中提取密钥。

I believe this can be accomplished with grep, but I am not sure on the correct way.

我相信这可以通过 grep 来完成,但我不确定正确的方法。

If there is a better way that does not have dependencies, I would be interested.

如果有没有依赖关系的更好方法,我会感兴趣。

Here is my example output:

这是我的示例输出:

{"data": {"name": "test", "id": "4dCYd4W9i6gHQHvd", "domains": ["www.test.domain.com", "test.domain.com"], "serverid": "bbBdbbHF8PajW221", "ssl": null, "runtime": "php5.6", "sysuserid": "4gm4K3lUerbSPfxz", "datecreated": 1474597357}, "actionid": "WXVAAHQDCSILMYTV"}

回答by Benjamin W.

If you have a grep that can do Perl compatible regular expressions (PCRE):

如果您有一个可以执行 Perl 兼容正则表达式 (PCRE) 的 grep:

$ grep -Po '"id": *\K"[^"]*"' infile.json
"4dCYd4W9i6gHQHvd"
  • -Penables PCRE
  • -oretains nothing but the match
  • "id": *matches "id"and an arbitrary amount of spaces
  • \Kthrows away everything to its left ("variable size positive look-behind")
  • "[^"]*"matches two quotes and all the non-quotes between them
  • -P启用 PCRE
  • -o只保留匹配项
  • "id": *匹配"id"和任意数量的空格
  • \K扔掉它左边的所有东西(“可变大小正向后视”)
  • "[^"]*"匹配两个引号和它们之间的所有非引号

If your grep can't do that, you an use

如果您的 grep 不能这样做,您可以使用

$ grep -o '"id": *"[^"]*"' infile.json | grep -o '"[^"]*"$'
"4dCYd4W9i6gHQHvd"

This uses grep twice. The result of the first command is "id": "4dCYd4W9i6gHQHvd"; the second command removes everything but a pair of quotes and the non-quotes between them, anchored at the end of the string ($).

这将使用 grep 两次。第一个命令的结果是"id": "4dCYd4W9i6gHQHvd"; 第二个命令删除除了一对引号和它们之间的非引号之外的所有内容,锚定在字符串 ( $)的末尾。

But, as pointed out, you shouldn't use grep for this, but a tool that can parse JSON – for example jq:

但是,正如所指出的,您不应该为此使用 grep,而是使用可以解析 JSON 的工具 - 例如jq

$ jq '.data.id' infile.json
"4dCYd4W9i6gHQHvd"

This is just a simple filter for the idkey in the dataobject. To get rid of the double quotes, you can use the -r("raw output") option:

这只是对象中id键的简单过滤器data。要去掉双引号,您可以使用-r("raw output") 选项:

$ jq -r '.data.id' infile.json
4dCYd4W9i6gHQHvd

jq can also neatly pretty print your JSON:

jq 还可以漂亮地打印您的 JSON:

$ jq . infile.json
{
  "data": {
    "name": "test",
    "id": "4dCYd4W9i6gHQHvd",
    "domains": [
      "www.test.domain.com",
      "test.domain.com"
    ],
    "serverid": "bbBdbbHF8PajW221",
    "ssl": null,
    "runtime": "php5.6",
    "sysuserid": "4gm4K3lUerbSPfxz",
    "datecreated": 1474597357
  },
  "actionid": "WXVAAHQDCSILMYTV"
}

回答by jasonleonhard

Just pipe your data to jqand select by keys

只需将您的数据jq通过管道传输到并按键选择

"data": {
    "name": "test",
    "id": "4dCYd4W9i6gHQHvd",
    "domains": [
      "www.test.domain.com",
      "test.domain.com"
    ],
    "serverid": "bbBdbbHF8PajW221",
    "ssl": null,
    "runtime": "php5.6",
    "sysuserid": "4gm4K3lUerbSPfxz",
    "datecreated": 1474597357
  },
  "actionid": "WXVAAHQDCSILMYTV"
} | jq '.data.id'     

# 4dCYd4W9i6gHQHvd

Tutorial Here

教程在这里

回答by Kamil Christ

I found myself that the best way is to use python, as it handles JSON natively and is preinstalled on most systems these days, unlike jq:

我发现自己最好的方法是使用 python,因为它本机处理 JSON 并且现在预装在大多数系统上,与 jq 不同:

$ python -c 'import sys, json; print(json.load(sys.stdin)["data"]["id"])' < infile.json
4dCYd4W9i6gHQHvd