28

In working with Elasticsearch on AWS EC2, I just hit an issue with bulk indexing. The ES _bulk endpoint requires the files to be basically JSON serial strings with \n terminators on each string; and what I have built using various web APIs and file pre/processing is pretty JSON ie., easily human readable.

Is there a simple shell script method to get all the pretty JSON simply concatenated into strings, without loading up some Java libraries or whatever? I can add tokens to the basic file during pre-processing to tag the desired \n breaks if that helps parsing, but if anyone has a tip on the toolset I would be grateful. I have a feeling there are scripts out there and I know there are some libraries, but I have not found any simple command line tools to do the unpretty printing so far.

4

4 回答 4

36

您可以尝试在 shell 中解析 JSON 的出色jq工具。要使用 jq 去美化打印,您可以使用以下任一方法:

cat pretty-printed.json | jq -c .
jq -c . pretty-printed.json

-c (或 --compact-output)告诉它打印不漂亮(这是默认设置)。这 ”。” 告诉它“按原样”返回 JSON 内容,除重新格式化外,未经修改。它被转储回标准输出,因此您可以重定向输出或将其通过管道传输到其他东西。

PS我正在寻找解决同样的问题并来到这个选项。

于 2015-10-17T00:31:29.630 回答
3

D_S_toowhite 的答案不是直接的答案,但它让我以正确的方式思考,即问题是删除所有空白。我找到了一种使用命令行工具 tr 删除所有空格的非常简单的方法:

tr -d [:space:] inputfile

:space: 标签删除所有空白、制表符、空格、垂直制表符等。所以一个漂亮的 JSON 输入是这样的:-

{
    "version" : "4.0",
    "success" : true,
    "result" :
    {
            "Focus" : 0.000590008,
            "Arc" : 12
    }
}

变成这个 JSON 序列字符串:

{"version":"4.0","success":true,"result":{"Focus":0.000590008,"Arc":12}}

我仍然必须解决 \n 终止符,但我认为这至少在我的特殊情况下现在是微不足道的,只需在使用 sed 关闭括号对之后附加。

于 2014-09-16T07:19:28.930 回答
1

您可以尝试使用正则表达式查找/替换:

  1. 查找内容: "^\s{2,}" 替换为 ""
  2. 查找什么 "\n" 替换 ""

看到这个:https ://github.com/dzhibas/SublimePrettyJson/issues/17

于 2014-09-16T02:07:20.180 回答
1

jsonlint在 npm 的帮助下很容易在命令行中启动和运行,打印出'no fluff' JSON 的简单方法是给它一个缩进字符“”。

jsonlint -t ""

作为对命令行用户的奖励,我一直使用它来获取粘贴缓冲区(在 Mac 上)并将它们转换为其他内容,例如:

交换 JSON linted 'compressed' 格式的缓冲区内容:

pbpaste | jsonlint -t "" | pbcopy

将缓冲区内容交换为漂亮打印的 JSON linted 格式:

pbpaste | jsonlint | pbcopy

您还可以将文件内容通过管道传输到文件的丑陋(和 JSON linted)版本:

cat data-pretty.json | jsonlint -t "" > data-ugly.json
于 2015-11-13T17:26:55.473 回答