bash 将 JSON 漂亮打印转换为一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39648222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:12:53  来源:igfitidea点击:

Converting JSON pretty print to one line

jsonlinuxbashawksed

提问by h.b

I have a command that I run and it gives an output like below:

我有一个我运行的命令,它提供如下输出:

{
"endpointApplications": {
    "App_Name": {
        "connectionState": "Disconnected",
        "connectionTime": "No connection was established",
        "linkAttributes": {
            "ackSettings": {
                "dataAckEnabled": "true",
                "dataAckTimeout": "5000",
                "dataNakRetryLimit": "0",
                "retransmitDelay": "500"
            },
            "keepAliveSettings": {
                "keepAliveAckTimeout": "5000",
                "keepAliveInterval": "30000"
            },
            "logTraffic": "false",
            "port": "9999",
            "role": "server"
        },
        "protocol": "snmp"
    }
},
"queueStats": {}
}

I would need the output to be in one line like below:

我需要输出在一行中,如下所示:

{"endpointApplications": {"app_name": {"connectionState": "Disconnected","connectionTime": "No connection was established","linkAttributes": {"ackSettings":{"dataAckEnabled": "true","dataAckTimeout": "5000","dataNakRetryLimit": "0","retransmitDelay": "500"},"keepAliveSettings":{"keepAliveAckTimeout": "5000","keepAliveInterval": "30000"},"logTraffic": "false","port": "9999","role": "server"},"protocol": "snmp"}},"queueStats":{}}

I tried using awk and sed combining different parameters but I can't get to work without losing the JSON format.

我尝试使用 awk 和 sed 结合不同的参数,但我无法在不丢失 JSON 格式的情况下开始工作。

采纳答案by P....

jqor any other jsonaware tool is best suited for json file manipulation.However here is awkbased solution.

jq或任何其他json感知工具最适合 json 文件操作。然而,这里是awk基于解决方案。

awk -v RS= '{=}1' input.json
{ "endpointApplications": { "App_Name": { "connectionState": "Disconnected", "connectionTime": "No connection was established", "linkAttributes": { "ackSettings": { "dataAckEnabled": "true", "dataAckTimeout": "5000", "dataNakRetryLimit": "0", "retransmitDelay": "500" }, "keepAliveSettings": { "keepAliveAckTimeout": "5000", "keepAliveInterval": "30000" }, "logTraffic": "false", "port": "9999", "role": "server" }, "protocol": "snmp" } }, "queueStats": {} }

Note: This solution is mainly for the legacy systems not having tools like jqand have no chance to get them installed due to some reasons.

注意:此解决方案主要针对没有此类工具jq且由于某些原因没有机会安装它们的旧系统。

回答by redneb

You should use jqfor stuff like that:

你应该使用jq来处理这样的事情:

jq -c . input.txt

An alternative quick a dirty solution would be to use sed& tr:

另一种快速肮脏的解决方案是使用sed& tr

sed -e 's/^ *//' < input.txt | tr -d '\n'

although I would recommend using jqwhich is designed for manipulating JSON. jqis like sedfor JSON. Manipulating JSON textually with sed/awk/etc is not guaranteed to produce semantically equivalent JSON.

尽管我建议使用jqwhich 是为操作 JSON 而设计的。jq就像sedJSON。使用sed/ awk/etc 以文本方式操作 JSON不能保证生成语义等效的 JSON。