在 JSON 中获取密钥

Get keys in JSON

我从外部系统得到以下 JSON 结果:

{
  "key1": "val1",
  "key2": "val2",
  "key3": "val3"
}

现在我想使用 JSONPath 显示所有键和所有值。因此,我正在寻找一些东西来获得 key1、key2 和 key3。另外我想使用 属性 的索引,e。 G。 $....[2].key 得到 "key3" 等等。 有没有办法做这样的事情?

我发现 tilda ~ 符号能够检索它所调用的值的键。因此,对于您的示例,查询如下:

$.*~

Returns这个:

[
  "key1",
  "key2",
  "key3"
]

另一个例子,如果我们有一个这样的 JSON 文档:

  {
  "key1": "val1",
  "key2": "val2",
  "key3": {
      "key31":"val31",
      "key32":"val32"
  }
}

这样的查询:

$.key3.*~

会 return 这个:

[
  "key31",
  "key32"
]

请务必注意,这些示例适用于 JSONPath.com and some other simulators/online tools, but on some they don't. It might come from the fact that I found out about the tilda(~) operator in the JSONPath plus 文档而非官方文档。