Skip to content

Instantly share code, notes, and snippets.

@thanoskoutr
Created February 16, 2023 12:08
Show Gist options
  • Save thanoskoutr/de8dc26d8a34f27a51719200628fa4a9 to your computer and use it in GitHub Desktop.
Save thanoskoutr/de8dc26d8a34f27a51719200628fa4a9 to your computer and use it in GitHub Desktop.
JSON formatter in pure bash
#!/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