将JSON漂亮打印转换为一行 [英] Converting JSON pretty print to one line

查看:2018
本文介绍了将JSON漂亮打印转换为一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行的命令,其输出如下:

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":{}}

我尝试使用awk并结合了不同参数的sed,但是在不丢失JSON格式的情况下我无法工作.

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

推荐答案

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

jq or any other json aware tool is best suited for json file manipulation.However here is awk based solution.

awk -v RS= '{$1=$1}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": {} }

注意:此解决方案主要用于没有诸如jq之类的工具且由于某些原因而没有机会安装它们的旧系统.

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

这篇关于将JSON漂亮打印转换为一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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