Skip to content

Instantly share code, notes, and snippets.

@ichux
Last active June 30, 2021 10:36
Show Gist options
  • Save ichux/89338a85e605df27f7b3879555c63601 to your computer and use it in GitHub Desktop.
Save ichux/89338a85e605df27f7b3879555c63601 to your computer and use it in GitHub Desktop.
echo json to formatted output
#!/bin/sh
printf "\033c"
echo '{
"company": "PITCH CARDINAL CODING LIMITED",
"rc_no": "1387119",
"established": "2017-01-25",
"address": "1B, MARKET STREET, OYINGBO"
}' | python -m json.tool # > result.json
echo ""
#
(cat <<EOF
{
"ip": "127.0.0.1",
"requestpath": "requestpath",
"httpmethod": "httpmethod",
"useragent": "Mozilla/5.0 (Windows NT 5.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0 `date +%s`",
"userplatform": "Linux",
"userbrowser": "Mozilla Firefox",
"userbrowserversion": "`date +%s`",
"referrer": "referrer-`date +%s`",
"requestargs": "requestargs-`date +%s`",
"postvalues": "postvalues-`date +%s`",
"errortype": "errortype-`date +%s`",
"errormsg": "errormsg-`date +%s`",
"when": "`date -u +"%Y-%m-%dT%H:%M:%S.00000"`",
"errortraceback": "errortraceback-`date +%s`",
"code": "code-`date +%s`",
"date": "`date -u +"%Y-%m-%dT%H:%M:%S.00000"`"
}
EOF
) | python -c "import sys, json; print(json.dumps(json.load(sys.stdin)))"
@ichux
Copy link
Author

ichux commented Oct 18, 2018

Run

python -m json.tool > output.json

To capture the result into a file named output.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment