Sign inSign up free

755. Build JSON Safely From Shell Variables

medium 20 XP ~10 minHands-on lab

The deploy notifier builds its webhook body with echo "{\"text\": \"$MSG\"}". It broke the day someone's commit message contained a quote, and worse, a crafted message could add extra JSON fields.

Write /home/student/bin/payload.sh NAME MESSAGE [TAG...].

Task

Print one JSON object (compact or pretty, any key order) with:

  • "service": NAME (a string),
  • "message": MESSAGE, exactly as given (any characters, including quotes, backslashes and newlines),
  • "tags": an array of the TAG arguments in order (an empty array when there are none),
  • "count": the number of tags, as a number.

The output must always be valid JSON, whatever the arguments contain. Exit 2 (usage on stderr) when NAME or MESSAGE is missing.

About this machine

Ubuntu 24.04 with no internet access. You are student with passwordless sudo. The grader checks the final state of the machine, not the commands you typed. Values like names and ports are generated for you.

Part of Shell Scripting for Automation. Next step: Turn JSON Logs Into Readable Lines

Teaching or writing about this? Embed this lab on your page

TerminalNot started

Your machine is ready to start

A fresh, isolated Ubuntu machine boots in a few seconds with the task already set up.

No account needed: 3 of 3 free tries left. Guest machines last 30 minutes.