如何在 jq 中在其自己的行中打印每个一级 json 字段?

如何在 jq 中在其自己的行中打印每个一级 json 字段?

当我使用以下命令漂亮地打印 json 文件时

cat xx.json | jq .

,它在自己的行中打印每个字段。例如,如果 json 文件包含 xy 坐标数组,这会导致输出非常长。

有没有一种方法可以用来jq在自己的行中打印 json 对象的每个一级字段(即根的直接子字段)?

更一般地说,有没有一种方法可以将 json 对象扩展到 level k,以便每个级别 k 的子级都打印在包含其后代的一行中(级别大于 k)。

-- 澄清:

一个小的说明性示例是:

echo '{"type":"MultiPolygon","coordinates":[[[[-94.9065,38.9884],[-94.8682,39.0596],[-94.6053,39.0432],[-94.6108,38.8460],[-94.6108,38.7365],[-94.9668,38.7365],[-95.0544,38.7365],[-95.0544,38.9829]]]]}"' | jq .

生成:

{
  "type": "MultiPolygon",
  "coordinates": [
    [
      [
        [
          -94.9065,
          38.9884
        ],
        [
          -94.8682,
          39.0596
        ],
        [
          -94.6053,
          39.0432
        ],
        [
          -94.6108,
          38.846
        ],
        [
          -94.6108,
          38.7365
        ],
        [
          -94.9668,
          38.7365
        ],
        [
          -95.0544,
          38.7365
        ],
        [
          -95.0544,
          38.9829
        ]
      ]
    ]
  ]
}

我想"coordinates"在一行中打印 的所有值及其每个同级的值(更一般地说,如果该coordinates字段位于根的第 k 级而不是第 1 级,我希望实现相同的效果)。

答案1

jq实用程序不提供对其漂亮打印的这种级别的控制。您可能可以使用的最简单的方法jq是输出序列化的结构部分:

$ jq '.coordinates[][] |= map(@json)' file
{
  "type": "MultiPolygon",
  "coordinates": [
    [
      [
        "[-94.9065,38.9884]",
        "[-94.8682,39.0596]",
        "[-94.6053,39.0432]",
        "[-94.6108,38.846]",
        "[-94.6108,38.7365]",
        "[-94.9668,38.7365]",
        "[-95.0544,38.7365]",
        "[-95.0544,38.9829]"
      ]
    ]
  ]
}

这会将数组中的数组替换.coordinates[][]为表示序列化数组的 JSON 编码字符串。如果不首先将序列化数组传递到fromjsonin ,则这将不再用于处理jq。但是,如果输出仅用于可视化,那么这可能是一个足够好的解决方案。

应用更高一级的序列化:

$ jq '.coordinates[] |= map(@json)' file
{
  "type": "MultiPolygon",
  "coordinates": [
    [
      "[[-94.9065,38.9884],[-94.8682,39.0596],[-94.6053,39.0432],[-94.6108,38.846],[-94.6108,38.7365],[-94.9668,38.7365],[-95.0544,38.7365],[-95.0544,38.9829]]"
    ]
  ]
}

有一种工具叫jtc,它的灵感来自jq,并为 JSON 文档提供了通常的漂亮打印和紧凑输出格式。它还具有“半紧凑”格式,以紧凑的方式显示最里面的对象:

$ jtc -tc file
{
   "coordinates": [
      [
         [
            [ -94.9065, 38.9884 ],
            [ -94.8682, 39.0596 ],
            [ -94.6053, 39.0432 ],
            [ -94.6108, 38.846 ],
            [ -94.6108, 38.7365 ],
            [ -94.9668, 38.7365 ],
            [ -95.0544, 38.7365 ],
            [ -95.0544, 38.9829 ]
         ]
      ]
   ],
   "type": "MultiPolygon"
}

我不太熟悉,jtc所以我不能立即说这个输出格式是否可配置(快速浏览一下用户指南表明它是不是完全按照您想要的方式进行配置)。另请注意,这jtc将按键对对象进行排序。


您也可能对。。。有兴趣jless如果您的目标是在终端中手动探索或查看数据。默认情况下它会向您显示这样的数据:

▼ {type: "MultiPolygon", coordinates: […]}
    type: "MultiPolygon"
  ▽ coordinates: [[[[…], […], […], […], […], […], […], […]]]]
    ▽ [0]: [[[…], […], […], […], […], […], […], […]]]
      ▽ [0]: [[…], […], […], […], […], […], […], […]]
        ▽ [0]: [-94.9065, 38.9884]
            [0]: -94.9065
            [1]: 38.9884
        ▽ [1]: [-94.8682, 39.0596]
            [0]: -94.8682
            [1]: 39.0596
        ▽ [2]: [-94.6053, 39.0432]
            [0]: -94.6053
            [1]: 39.0432
        ▽ [3]: [-94.6108, 38.846]
            [0]: -94.6108
            [1]: 38.846
        ▽ [4]: [-94.6108, 38.7365]
            [0]: -94.6108
            [1]: 38.7365
        ▽ [5]: [-94.9668, 38.7365]
            [0]: -94.9668
            [1]: 38.7365
        ▽ [6]: [-95.0544, 38.7365]
            [0]: -95.0544
            [1]: 38.7365
        ▽ [7]: [-95.0544, 38.9829]
            [0]: -95.0544
            [1]: 38.9829

...每个部分都可以使用箭头键轻松折叠。

相关内容