Skip to content

Instantly share code, notes, and snippets.

@waltervargas
Created October 26, 2018 16:02
Show Gist options
  • Save waltervargas/0f6d1ba48c802d15e98baf13937e5857 to your computer and use it in GitHub Desktop.
Save waltervargas/0f6d1ba48c802d15e98baf13937e5857 to your computer and use it in GitHub Desktop.
Bash JSON check
#!/bin/sh
_json_valid(){
local _STD_OUT_ERR
_STD_OUT_ERR=$( { jq type /dev/stdin <<< "${1}"; } 2>&1 )
}
read -d '' -r _JSON <<EOB
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}
EOB
if _json_valid "${_JSON}"; then
echo "JSON valid for jq :)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment