jq的无效数字文字

Gai*_*ius 5 json jq

我想使用jq预处理来自第三方系统的大量JSON,但是我在编写查询时遇到困难,测试用例如下:

$ cat test.json
{
  "a": "b",
  "c": "d",
  "e": {
         "1": {
            "f": "g",
            "h": "i"
          }
       }
}
$ cat test.json|jq .e.1.f
jq: error: Invalid numeric literal at EOF at line 1, column 3 (while parsing '.1.') at <top-level>, line 1:
.e.1.f
Run Code Online (Sandbox Code Playgroud)

我在这里如何得到“ g”作为输出?或如何将1强制转换为“ 1”,以便正确处理?

Ber*_*tel 6

来自jq 手册

您还可以使用 .["foo"] 之类的语法查找对象的字段(上面的 .foo 是其简写版本,但仅适用于类似标识符的字符串)。

-r如果您想要原始输出,您还需要引号并使用:

jq -r '.e["1"].f' test.json
Run Code Online (Sandbox Code Playgroud)

  • 您也不一定需要使用方括号,它可以是简单的: `.e."1".f` (3认同)