Created
February 16, 2023 12:08
-
-
Save thanoskoutr/de8dc26d8a34f27a51719200628fa4a9 to your computer and use it in GitHub Desktop.
JSON formatter in pure bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function jsonf() { | |
file=$1 | |
indent=0 | |
while IFS= read -r -n1 char; do | |
case $char in | |
("{" | "[") echo "$char"; ((indent+=4)); printf "%${indent}s";; | |
("}" | "]") echo ""; ((indent-=4)); printf "%${indent}s$char";; | |
(",") echo "$char"; printf "%${indent}s";; | |
(*) printf "%s" "$char";; | |
esac | |
done < <(sed -e 's/}/}\n/g' -e 's/{/{\n/g' $file) | |
} | |
jsonf $1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment