duncan­lock­.net

Templating JSON data into a Variable, in Bash

Building a JSON string in Bash can be pretty fiddly and annoying, with quoting and formatting and variable substitution, etc…​

If you only have a little JSON, using a printf template works well, like this:

#!/usr/bin/env bash

# Your printf template string
template='{ "state": "%s", "key": "%s", "description": "%s", "url": "%s" }'
# Variable to store your rendered template JSON string
data=""

# Some variables to substitute into the template
BITBUCKET_STATE="FAILED"
BITBUCKET_KEY="ci/gitlab-ci/failure"
BITBUCKET_DESCRIPTION="The build failed."
CI_PROJECT_URL="https://gitlab.example.com/project_name/repo_name"
CI_JOB_ID="123"

# Render the template, substituting the variable values and save the result into $data
printf -v data "$template" "$BITBUCKET_STATE" "$BITBUCKET_KEY" "$BITBUCKET_DESCRIPTION" "$CI_PROJECT_URL/-/jobs/$CI_JOB_ID"

# Print it out
echo "$data"

This outputs the following:

{ "state": "FAILED", "key": "ci/gitlab-ci/failure", "description": "The build failed.", "url": "https://gitlab.example.com/project_name/repo_name/-/jobs/123" }

If you want it multi-line, you can just put newlines in the template - but then your $data variable will have newlines in, so be careful passing it on the command line, etc…​

References


Related Posts