【问题标题】:Use jq to Format Certain Fields as Compact?使用 jq 将某些字段格式化为紧凑?
【发布时间】:2017-03-11 00:04:27
【问题描述】:

jq 是漂亮打印任意 JSON 的最佳选择吗?

cat my.json | jq . 漂亮地打印给定的 JSON,但将每个字段扩展为单独的行。

但是如果某些字段是重复的,例如点列表,该怎么办?与模式匹配的字段如何在一行中使用--compact-output 格式化?

例如,将下面的“coords”和“list”字段格式化为一行:

 [
   { 
      "field1": {
        "a": "",
        "b": ""
        "list": [{ "name": "x", "score": 1, "rect": { "x": 156, "y": 245, "w": 35, "h": 45 }, ... ]
      },
      "field2": 2,
      "coords": [{ "x": 100, "y": 400 },{ "x": 100, "y": 0 }]
    },
    ....
 ]

--compact-output 格式的字段可以换行(无需中断这些长行)。

【问题讨论】:

  • 我不知道解决方案,但这是一个已知问题。 github.com/stedolan/jq/issues/643
  • 我想可以编写一个过滤器来手动格式化某些对象,如果它是流式传输的。感兴趣的字段将被输出到缓冲区以进行压缩,而其他字段将被转储到输出。然后当到达字段的末尾时,输出缓冲区。作为一个流,您可以获得 json 对象和数组的开始和停止及其路径。那么它当然必须是原始输出。

标签: json pretty-print jq


【解决方案1】:

可以用 jq 本身编写一个宽度受限的 JSON 漂亮打印机。 这是一个漂亮的打印机,它说明了如何做到这一点,尽管在目前的化身中,它的用处有限。

ppArray(indent; incr; width) 将发出一个 JSON 字符串流 加起来相当于输入的tostring 值。为了 健壮性,它将始终充当漂亮的打印机,即使 违反了宽度限制。

如果输入是一个数组,并且如果没有任何元素(或递归 它们的元素)包含任何大对象或长字符串,然后假设参数的值被合理选择并且嵌套相对于这些参数不是太深, 每个发出的字符串不应超过“宽度”。

# indent is the initial indentation level;
# incr is the number of spaces to add for one additional indentation level;
# width is the target maximum width.
#
def ppArray(indent; incr; width):
  # The inner function produces an array of unindented strings.
  def ppArray_(incr_; width_):
    tostring as $tostring
    | if $tostring|length <= (width_ - incr_) then [$tostring]
      else reduce .[] as $i
        ([];  
         ($i|tostring) as $is
          | if length == 0 then [ $is ]
            else .[-1] as $s
            | ($s|length) as $n
            | ($is|length) as $isl
            | if $n + $isl <= (width_ - incr_)
             then .[-1] = ($s + ", " + $is)
              elif ($i|type) == "array"
             then (.[-1]+=",") + [ $i | ppArray(0; incr_; width_ - incr_) ]
              else  (.[-1]+=",") + [ $is ]
              end 
            end )
      end;

    (" " * indent) as $indentation
    | if type == "array" 
      then ppArray_(incr; width - indent)
           | $indentation + "[",
           (.[] | ($indentation + "  " + . )),
           $indentation + "]"
    else $indentation + tostring
    end
;

示例:

[range(0;16)]
|
(ppArray(0; 2; 10)),
"::",
([{a:1}, {b:2}, {c:3}]  | ppArray(0; 2; 10)),
"::",
(.[2]=[range(0;10)]) | ppArray(0; 2; 10)

调用:

jq -nrf pp.jq

输出:

[
  0, 1, 2, 3,
  4, 5, 6, 7,
  8, 9, 10,
  11, 12, 13,
  14, 15
]
::
[
  {"a":1},
  {"b":2},
  {"c":3}
]
::
[
  0, 1,
  [
    0, 1, 2,
    3, 4, 5,
    6, 7, 8,
    9
  ], 3, 4, 5,
  6, 7, 8, 9,
  10, 11, 12,
  13, 14, 15
]

【讨论】:

  • 好主意!比预期的要复杂,但具有创新性。我真正想要的是一种将某些字段放在一行上的方法,而所有其他字段放在单独的一行上。用 jq 解决会更简单吗?
  • 对于结合 jq 和后处理步骤的实用方法,请参阅stackoverflow.com/questions/46805833/…
猜你喜欢
  • 1970-01-01
  • 2021-11-21
  • 2017-07-19
  • 2014-02-19
  • 2017-01-01
  • 2020-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多