使用jq将某些字段格式化为紧凑格式? [英] Use jq to Format Certain Fields as Compact?

查看:77
本文介绍了使用jq将某些字段格式化为紧凑格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

cat my.json | jq . pretty-prints the given JSON, but expands every field on a separate line.

但是,如果某些字段是重复的,例如点列表,该怎么办?

But what if some of the fields are repetitive, such as a list of points? How can fields that match a pattern be formatted on a single line with --compact-output?

例如,将下面的坐标"和列表"字段设置为一行:

For example, format "coords" and "list" fields below on a single line:

 [
   { 
      "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格式化的字段可以换行(不需要打破这些长行).

The fields formatted with --compact-output can wrap (no need to break these long lines).

推荐答案

可以使用jq本身编写宽度受限的JSON漂亮打印机. 这是一个漂亮的打印机,说明了如何完成此操作,尽管就目前的形式而言,它的用处有限.

A constrained-width JSON pretty-printer could be written in jq itself. Here is a pretty-printer which illustrates how this could be done, though in its present incarnation, it is of limited usefulness.

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

ppArray(indent; incr; width) will emit a stream of JSON strings that together are equivalent to the tostring value of the input. For robustness, it will always act as a pretty-printer, even if the width restriction is violated.

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

If the input is an array, and if none of its elements (or recursively their elements) contains any large objects or long strings, then assuming the values of the parameters are reasonably chosen and that nesting is not too deep relative to these parameters, each emitted string should be no longer than "width".

# 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将某些字段格式化为紧凑格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆