Search code examples
jsonshellposix

Don't remove white space from `value` in minified json


Some backgroup :- I can't use jq or other python/ruby solution and i have to use bash only (POSIX)

I have json in below format :-

{
    "argumets": {
        "arg1": "-write -nrFiles 1000 -size1000",
        "arg2": "-read -nrFiles 1000 -size1000",
        "arg3": "-clean"
    }
}

I am using this tr command to minify json :-

tr -d '[:space:]' < input_json.file > output_json.file

And this is the output i get :-

{"argumets":{"arg1":"-write-nrFiles1000-size1000","arg2":"-read-nrFiles1000-size1000","arg3":"-clean"}}

Here i am loosing space in value side like -write-nrFiles1000-size1000

Any other suggestion or improvements i can do to avoid above scenario?


Solution

  • Here is the solution i worked out for my problem. Hope it helps others too.

    awk '{$1=$1;print}' file.json | sed 's/: /:/' | tr -d '\n'
    

    Credit to few answers/comment on stackoverflow which helped me:-

    https://unix.stackexchange.com/a/205854/404618

    Don't remove white space from `value` in minified json