As developers we often need to take minified JSON and XML and pretty print it so it is human readable. A multitude of websites provide a free service where you can enter minified JSON or XML and it will format it the data so it is human readable. The problem is this can lead to leaking data causing security incidents. The solution is to write a couple of bash scripts to pretty print.
A big thanks to Campovski's answer on stackoverflow for the JSON and Ben Noland's answer on stackoverflow and Anton I. Sipos' comment for the XML
I took these answers a little further and created a bash script that accepts arguments so you can pretty print
- A file
- The result returned form an API
- A string
- Data piped in
Both scripts have the same usage
Format a file with the -f flag
JSON
./pretty_json.sh -f <name_of_file>
XML
./pretty_xml.sh -f <name_of_file>
Format the return value of an API with the -u flag
JSON
./pretty_json.sh -u www.api.com
XML
./pretty_xml.sh -u www.api.com
Format a string with the -s flag
JSON
./pretty_json.sh -s '{"name":"Luke","city":"Sydney","country":"Australia"}'
XML
./pretty_xml.sh -s '<xml><name>Luke</name><city>Sydney</city><country>Australia</country></xml>'
Format a string with no flag set
JSON
./pretty_json.sh '{"name":"Luke","city":"Sydney","country":"Australia"}'
XML
./pretty_xml.sh '<xml><name>Luke</name><city>Sydney</city><country>Australia</country></xml>'
Pipe a value to be formated
JSON
echo '{"name":"Luke","city":"Sydney","country":"Australia"}' | ./pretty_json.sh
XML
echo '<xml><name>Luke</name><city>Sydney</city><country>Australia</country></xml>' | ./pretty_xml.sh
The scripts are below
pretty_json.sh
#!/bin/bash
options=$(getopt f:s:u: "$@")
[ $? -eq 0 ] || {
echo "Invalid option. No action taken. Script will now exit"
exit 1
}
for i
do
case "$i" in
-f)
# file name provided
shift;
python -m json.tool $1;
exit 0;;
-s)
# string provided
shift;
echo "$1" | python -m json.tool;
exit 0;;
-u)
# url provided
shift;
curl "$1" | python -m json.tool;
exit 0;;
esac
done
if [ -z "$1" ]
then
# Data piped in
echo "`python -m json.tool < /dev/stdin`";
else
# String provided as the first argument
echo "$1" | python -m json.tool;
fi
pretty_xml.sh
#!/bin/bash
options=$(getopt f:s:u: "$@")
[ $? -eq 0 ] || {
echo "Invalid option. No action taken. Script will now exit"
exit 1
}
for i
do
case "$i" in
-f)
# file name provided
shift;
#python -c "import sys;import xml.dom.minidom;f=open('$1', 'r');print(xml.dom.minidom.parseString(f).toprettyxml())";
echo "$1" | python -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();f=open(s.strip(), "r");print(xml.dom.minidom.parseString(f.read()).toprettyxml())'
exit 0;;
-s)
# string provided
shift;
echo "$1" | python -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();print(xml.dom.minidom.parseString(s).toprettyxml())';
exit 0;;
-u)
# url provided
shift;
curl "$1" | python -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();print(xml.dom.minidom.parseString(s).toprettyxml())';
exit 0;;
esac
done
if [ -z "$1" ]
then
# Data piped in
#echo "`python -m json.tool < /dev/stdin`";
python -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();print(xml.dom.minidom.parseString(s).toprettyxml())'
else
# String provided as the first argument
echo "$1" | python -c 'import sys;import xml.dom.minidom;s=sys.stdin.read();print(xml.dom.minidom.parseString(s).toprettyxml())';
fi
# source https://stackoverflow.com/a/1920585/409119
http://fashionpeony.com/ merupakan agen Togel Online dengan pasaran resmi seperti Togel Hongkong Pools, Togel Singapore SGP terpercaya di Indonesia
ReplyDeleteHi Luke:
ReplyDeleteHope this email finds you well : )
My name is Chia, I found your profile on LinkedIn, I am currently running a platform called BrewCareer, which aims to help people to find their career mentors who will be coaching and guiding them. We think your experience and knowledge makes you a perfect mentor for many mentee who are seeking for an inspiring way to approach their career goal.
If you have any interest in becoming our mentor, please check our website and sign up as mentor, it will be our pleasure to have you to join our community.
https://brewcareer.com/
Look forward to see you soon, cheers.
Best regards,
Chia