Converting JSON pretty print to one line

前端 未结 2 585
野趣味
野趣味 2021-02-04 06:20

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

{
\"endpointApplications\": {
    \"App_Name\": {
        \"connectionState\": \"Disconnected\",
          


        
相关标签:
2条回答
  • 2021-02-04 06:51

    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.

    0 讨论(0)
  • 2021-02-04 06:57

    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.

    0 讨论(0)
提交回复
热议问题