Methods and Best Practices for Safely Building JSON Strings in Bash

Nov 24, 2025 · Programming · 6 views · 7.8

Keywords: Bash scripting | JSON generation | jq tool | character escaping | Shell programming

Abstract: This article provides an in-depth exploration of various methods for constructing JSON strings in Bash scripts, with a focus on the security risks of direct string concatenation and a detailed introduction to the safe solution using the jq tool. By comparing the advantages and disadvantages of different approaches and incorporating specific code examples, it elucidates key technical aspects such as character escaping and data validation, offering developers a comprehensive JSON generation solution. The article also extends the discussion to other tools like printf and jo, helping readers choose the most suitable implementation based on their actual needs.

Introduction

In modern software development, JSON (JavaScript Object Notation) has become the de facto standard format for data exchange. In Bash script programming, it is often necessary to embed variable values into JSON strings. However, due to JSON's strict requirements for character escaping and formatting, directly concatenating strings often leads to various issues.

Problem Analysis

Let's first analyze a typical erroneous example:

#!/bin/sh

BUCKET_NAME=testbucket
OBJECT_NAME=testworkflow-2.0.1.jar
TARGET_LOCATION=/opt/test/testworkflow-2.0.1.jar

JSON_STRING='{"bucketname":"$BUCKET_NAME"","objectname":"$OBJECT_NAME","targetlocation":"$TARGET_LOCATION"}'

echo $JSON_STRING

This code has several key issues: first, variables within single quotes are not expanded by Bash; second, even if double quotes are used, if variable values contain special characters such as quotes, backslashes, or newlines, the generated JSON will become invalid; finally, there are syntax errors in the code, such as extra quotes.

jq Tool Solution

jq is a lightweight and flexible command-line JSON processor specifically designed for handling JSON data. Using jq to construct JSON strings is the safest and most reliable method:

BUCKET_NAME=testbucket
OBJECT_NAME=testworkflow-2.0.1.jar
TARGET_LOCATION=/opt/test/testworkflow-2.0.1.jar

JSON_STRING=$( jq -n \
                  --arg bn "$BUCKET_NAME" \
                  --arg on "$OBJECT_NAME" \
                  --arg tl "$TARGET_LOCATION" \
                  '{bucketname: $bn, objectname: $on, targetlocation: $tl}' )

The core advantages of this method include:

printf Alternative

For simple use cases, the printf command can be used as a lightweight alternative:

JSON_FMT='{"bucketname":"%s","objectname":"%s","targetlocation":"%s"}\n'
printf "$JSON_FMT" "$BUCKET_NAME" "$OBJECT_NAME" "$TARGET_LOCATION"

The advantages of this method are simplicity and directness, with no dependency on external tools. However, note that:

jo Tool Extension

jo, mentioned in the reference article, is another tool specifically designed for creating JSON objects:

bin=$(cat next_entry)
outdir=/tmp/cpupower/$bin
json=$( jo hostname=localhost outdir="$outdir" port=20400 size=100000 )

Features of jo include:

Character Escaping and Data Security

Character escaping is a crucial consideration when constructing JSON. Characters that require special handling include:

Using professional tools like jq or jo automatically handles these escaping needs, avoiding errors from manual processing.

Best Practices Summary

Based on the above analysis, we summarize the following best practices:

  1. Prioritize jq: For production environment scripts, jq provides the most comprehensive security guarantees.
  2. Consider Tool Availability: In restricted environments, printf can serve as a backup solution.
  3. Validate Input Data: When handling user input or external data, appropriate validation should be performed.
  4. Error Handling: Add proper error checks to ensure the reliability of the JSON generation process.
  5. Performance Considerations: For high-frequency invocation scenarios, evaluate the performance impact of different methods.

Conclusion

Choosing the correct method is crucial when building JSON strings in Bash. Although direct string concatenation may seem simple, it hides serious security and compatibility risks. By using professional JSON processing tools like jq, not only can you ensure that the generated JSON conforms to standards, but you can also significantly improve the robustness and maintainability of the code. Developers should select the most suitable implementation based on specific requirements and environmental constraints.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.