So having spent a couple of days remembering how to code all kinds of Unix tools like sed, awk, and grep, learning a couple of relatively newer ones like curl (I know, right? I am even breaking out gcc for the first time in maybe 20 years, but it is all coming back quickly) and have been making good progress on building a little JSON DB for use with Elasticsearch on AWS EC2.
因此,花了几天时间来记住如何编写各种Unix工具,如sed、awk和grep,学习了一些相对较新的工具,如curl(我知道,对吧?)我甚至在20年的时间里第一次突破了gcc,但是它很快就会回来了)并且在构建一个小的JSON DB以在AWS EC2上使用弹性搜索方面取得了良好的进展。
But I just hit an issue with bulk indexing, in that 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.
但是我刚刚遇到了批量索引的问题,ES _bulk端点要求文件基本上是JSON串行字符串,每个字符串上有n个终止符;我使用各种web api和文件预/处理构建的是漂亮的JSON ie。,轻松人类可读的。
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 as I am this tiny step away from joining up the ends of the project. 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.
是否有一个简单的shell脚本方法可以将所有漂亮的JSON简单地连接到字符串中,而不需要加载一些Java库或其他什么?在预处理过程中,我可以向基本文件添加令牌,以标记所需的\n中断(如果这有助于解析的话),但是如果有人对工具集有提示的话,我将非常感激,因为我离加入项目的结束只有一步之遥。我有一种感觉,那里有脚本,我知道有一些库,但我还没有找到任何简单的命令行工具来完成不漂亮的打印。
Many thanks for any tips,
非常感谢你的建议,
Cheers
干杯
Sid
Sid
4 个解决方案
#1
8
You can try the great jq tool for parsing JSON in the shell. To de-pretty print with jq, you can use either method below:
您可以尝试在shell中解析JSON的伟大的jq工具。要想用jq来减少打印效果,你可以使用以下两种方法:
cat pretty-printed.json | jq -c .
jq -c . pretty-printed.json
the -c (or --compact-output) tells it to not pretty print (which is the default). The "." tells it to return the JSON content "as is" unmodified other than the reformatting. It gets dumped back to stdout, so you can redirect output or pipe it to something else.
-c(或-compac -output)告诉它不需要漂亮的打印(默认)。“。”告诉它返回JSON内容,因为“除了重新格式化外没有修改”。它将被转储回stdout,因此您可以重定向输出或将其传输到其他地方。
P.S. I was looking to address the same problem and came to this option.
附注:我也想解决同样的问题,于是就有了这个选择。
#2
2
The answer from D_S_toowhite was not a direct answer but it set me thinking in the right way i.e., the problem is to remove all the white space. I found a very simple way to remove all white space using command line tool tr:
D_S_toowhite的答案不是直接的,但它让我以正确的方式思考。问题是要移除所有的空白。我发现了一种非常简单的方法,可以使用命令行工具tr删除所有空白:
tr -d [:space:] inputfile
The :space: tag removes all white space, tabs, spaces, vertical tabs etc. So a pretty JSON input like this:-
空格:标签删除所有的空格、制表符、空格、垂直制表符等等
{
"version" : "4.0",
"success" : true,
"result" :
{
"Focus" : 0.000590008,
"Arc" : 12
}
}
becomes this JSON serial string:
变成这个JSON串行字符串:
{"version":"4.0","success":true,"result":{"Focus":0.000590008,"Arc":12}}
I still have to solve the \n terminator but I think that is trivial now at least in my special case, just append after closing bracket pair using sed.
我仍然需要解决\n终止符,但我认为这在我的特殊情况下是微不足道的,仅仅在使用sed结束括号对之后追加。
Many thanks for the suggestion.
非常感谢你的建议。
Cheers
干杯
Sid
Sid
#3
0
You can try find/replace using regexp:
您可以尝试使用regexp查找/替换:
- Find what: "^\s{2,}" replace to ""
- 找到:“^ \ s { 2,}”代替,“
- Find what "\n" replace ""
- 查找“\n”替换“”
See this: https://github.com/dzhibas/SublimePrettyJson/issues/17
看到这个:https://github.com/dzhibas/SublimePrettyJson/issues/17
#4
0
jsonlint is easy to get up and running in the command line with the help of npm, and a simple way to print out 'no fluff' JSON is to give it an indentation character of "".
在npm的帮助下,jsonlint很容易在命令行中启动和运行,而打印“no fluff”JSON的简单方法就是给它一个缩进字符。
jsonlint -t ""
As a bonus for command line users, I use this all the time to take paste buffers (on a Mac) and convert them into something else, for instance:
作为命令行用户的额外奖励,我一直使用这一功能,在Mac上使用粘贴缓冲区(在Mac上),并将其转换为其他内容,例如:
Swap buffer contents for a JSON linted 'compressed' format:
将缓冲内容交换成JSON链接的“压缩”格式:
pbpaste | jsonlint -t "" | pbcopy
Swap buffer contents for a pretty printed JSON linted format:
将缓冲内容交换为漂亮打印的JSON linted格式:
pbpaste | jsonlint | pbcopy
You could also pipe file contents to an ugly (and JSON linted) version of the file:
您还可以将文件内容导入一个丑陋的(和JSON链接的)文件版本:
cat data-pretty.json | jsonlint -t "" > data-ugly.json
#1
8
You can try the great jq tool for parsing JSON in the shell. To de-pretty print with jq, you can use either method below:
您可以尝试在shell中解析JSON的伟大的jq工具。要想用jq来减少打印效果,你可以使用以下两种方法:
cat pretty-printed.json | jq -c .
jq -c . pretty-printed.json
the -c (or --compact-output) tells it to not pretty print (which is the default). The "." tells it to return the JSON content "as is" unmodified other than the reformatting. It gets dumped back to stdout, so you can redirect output or pipe it to something else.
-c(或-compac -output)告诉它不需要漂亮的打印(默认)。“。”告诉它返回JSON内容,因为“除了重新格式化外没有修改”。它将被转储回stdout,因此您可以重定向输出或将其传输到其他地方。
P.S. I was looking to address the same problem and came to this option.
附注:我也想解决同样的问题,于是就有了这个选择。
#2
2
The answer from D_S_toowhite was not a direct answer but it set me thinking in the right way i.e., the problem is to remove all the white space. I found a very simple way to remove all white space using command line tool tr:
D_S_toowhite的答案不是直接的,但它让我以正确的方式思考。问题是要移除所有的空白。我发现了一种非常简单的方法,可以使用命令行工具tr删除所有空白:
tr -d [:space:] inputfile
The :space: tag removes all white space, tabs, spaces, vertical tabs etc. So a pretty JSON input like this:-
空格:标签删除所有的空格、制表符、空格、垂直制表符等等
{
"version" : "4.0",
"success" : true,
"result" :
{
"Focus" : 0.000590008,
"Arc" : 12
}
}
becomes this JSON serial string:
变成这个JSON串行字符串:
{"version":"4.0","success":true,"result":{"Focus":0.000590008,"Arc":12}}
I still have to solve the \n terminator but I think that is trivial now at least in my special case, just append after closing bracket pair using sed.
我仍然需要解决\n终止符,但我认为这在我的特殊情况下是微不足道的,仅仅在使用sed结束括号对之后追加。
Many thanks for the suggestion.
非常感谢你的建议。
Cheers
干杯
Sid
Sid
#3
0
You can try find/replace using regexp:
您可以尝试使用regexp查找/替换:
- Find what: "^\s{2,}" replace to ""
- 找到:“^ \ s { 2,}”代替,“
- Find what "\n" replace ""
- 查找“\n”替换“”
See this: https://github.com/dzhibas/SublimePrettyJson/issues/17
看到这个:https://github.com/dzhibas/SublimePrettyJson/issues/17
#4
0
jsonlint is easy to get up and running in the command line with the help of npm, and a simple way to print out 'no fluff' JSON is to give it an indentation character of "".
在npm的帮助下,jsonlint很容易在命令行中启动和运行,而打印“no fluff”JSON的简单方法就是给它一个缩进字符。
jsonlint -t ""
As a bonus for command line users, I use this all the time to take paste buffers (on a Mac) and convert them into something else, for instance:
作为命令行用户的额外奖励,我一直使用这一功能,在Mac上使用粘贴缓冲区(在Mac上),并将其转换为其他内容,例如:
Swap buffer contents for a JSON linted 'compressed' format:
将缓冲内容交换成JSON链接的“压缩”格式:
pbpaste | jsonlint -t "" | pbcopy
Swap buffer contents for a pretty printed JSON linted format:
将缓冲内容交换为漂亮打印的JSON linted格式:
pbpaste | jsonlint | pbcopy
You could also pipe file contents to an ugly (and JSON linted) version of the file:
您还可以将文件内容导入一个丑陋的(和JSON链接的)文件版本:
cat data-pretty.json | jsonlint -t "" > data-ugly.json