从JSON中查找key的值

时间:2021-02-20 08:56:45

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

我想从这一行JSON中提取“id”键。

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"}

1 个解决方案

#1


13  

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

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

$ grep -Po '"id": *\K"[^"]*"' infile.json
"4dCYd4W9i6gHQHvd"
  • -P enables PCRE
  • -P启用PCRE

  • -o retains nothing but the match
  • - 除了比赛之外什么也没有留下

  • "id": * matches "id" and an arbitrary amount of spaces
  • “id”:*匹配“id”和任意数量的空格

  • \K throws away everything to its left ("variable size positive look-behind")
  • \ K扔掉了左边的一切(“可变尺寸正面后视”)

  • "[^"]*" matches two quotes and all the non-quotes between them
  • “[^”] *“匹配两个引号和它们之间的所有非引号

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 id key in the data object.

这只是数据对象中id键的简单过滤器。

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"
}

#1


13  

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

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

$ grep -Po '"id": *\K"[^"]*"' infile.json
"4dCYd4W9i6gHQHvd"
  • -P enables PCRE
  • -P启用PCRE

  • -o retains nothing but the match
  • - 除了比赛之外什么也没有留下

  • "id": * matches "id" and an arbitrary amount of spaces
  • “id”:*匹配“id”和任意数量的空格

  • \K throws away everything to its left ("variable size positive look-behind")
  • \ K扔掉了左边的一切(“可变尺寸正面后视”)

  • "[^"]*" matches two quotes and all the non-quotes between them
  • “[^”] *“匹配两个引号和它们之间的所有非引号

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 id key in the data object.

这只是数据对象中id键的简单过滤器。

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"
}