Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting JSON pretty print to one line

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.

like image 775
h.b Avatar asked Sep 22 '16 20:09

h.b


People also ask

How do I print JSON in one line?

Step 1: Be prepared with the json code which you want to convert. Step 2: Copy the JSON (whichever you want to convert). Step 3: Paste the copied json code in the space provided. Step 4: Finally click the "convert" option to get the text converted in a one line without space.

How do you convert multiple lines to one line?

Select the lines you want to join ( Ctrl + A to select all) Choose Edit -> Line Operations -> Join Lines.

Can JSON be single line?

You can read JSON files in single-line or multi-line mode.

How do I print a pretty JSON object?

Use JSON. stringify(obj) method to convert JavaScript objects into strings and display it. Use JSON. stringify(obj, replacer, space) method to convert JavaScript objects into strings in pretty format.


2 Answers

You should use jq for stuff like that:

jq -c . input.txt

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

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

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

like image 142
redneb Avatar answered Sep 20 '22 04:09

redneb


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

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.

like image 28
P.... Avatar answered Sep 21 '22 04:09

P....