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.
You should use jq for stuff like that:
An alternative quick a dirty solution would be to use
sed
&tr
:although I would recommend using
jq
which is designed for manipulating JSON.jq
is likesed
for JSON. Manipulating JSON textually withsed
/awk
/etc is not guaranteed to produce semantically equivalent JSON.jq
or any otherjson
aware tool is best suited for json file manipulation.However here isawk
based solution.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.