apimon -c apimon.yml | # Prints JSON messages
while IFS= read -r json # Read line by line. $json holds the line content
do
# Send JSON over a HTTP Post request
echo $json | curl -H "Content-Type: application/json" -X POST -d @- http://yourloggingserver:4321
done
echo $json | sed -E 's/"/\\"/g' # Escapes all " to \"
echo $json |
# Replace the ending of a JSON string (closing curly brace before line end) with an
# added key-value pair and new closing curly brace.
# Replace key with any string. value is a awk variable. It is concatenated without
# any special operator: "foo"var"bar".
awk -v value=$(echo 'foo') '{gsub(/}$/,",\"key\":\""value"\"}"); print}'
If you wish to insert an escaped JSON string as a value:
echo $json |
# You need to double escape the JSON!
awk -v value=$(echo $some_other_json | sed -E 's/"/\\\\"/g') '{gsub(/}$/,",\"key\":\""value"\"}"); print}'
Take a look at jq if you like to manipulate JSON a lot easier:
echo $json | jq '. + { key: "value" }'