Using printf to write variable values to JSON strings in Bash scripts
文章描述了作者在从Jamf Pro Classic API的XML使用转向Jamf Pro API的JSON过程中遇到的挑战,并通过使用Bash脚本中的`printf`命令解决了将变量值正确写入JSON字符串的问题。 2025-12-22 18:14:32 Author: derflounder.wordpress.com(查看原文) 阅读量:4 收藏

Home > Jamf Pro API, Jamf Pro Classic API, Scripting > Using printf to write variable values to JSON strings in Bash scripts

Using printf to write variable values to JSON strings in Bash scripts

As part of my personal changeover from using the Jamf Pro Classic API to using the Jamf Pro API, I’ve needed to change from using XML for the Jamf Pro Classic API to now using JSON for the Jamf Pro API. In particular, one of my issues has been figuring out how to properly write variables to JSON when updating something on the Jamf Pro server using the Jamf Pro API. As always when writing scripts, there’s usually multiple ways to solve a problem and I figured out a solution to mine using the printf command. For more details, please see below the jump.

As an example, I had written a post a while back about updating the asset tag number of a Jamf Pro computer inventory record using a script. As part of that script, I was using a command similar to the one below to send the updated asset tag information to the Jamf Pro server, defined in a variable named asset_tag_number_goes_here.


As part of that command, the following XML string is being sent with the asset_tag_number_goes_here variable being included:


"<computer><general><asset_tag>${asset_tag_number_goes_here}</asset_tag></general></computer>"

The XML string is double quoted, so my script knows to read in the value from the asset_tag_number_goes_here variable because the variable is referenced using the $ character.

When I updated my script to use the Jamf Pro API and JSON, my command now looked similar to this:


My JSON string looked like this:


'{"general":{"assetTag":"${asset_tag_number_goes_here}"}}'

However, that didn’t work right and the asset_tag_number_goes_here variable’s value wasn’t being read correctly. Why? Because the JSON string uses single quotes to enclose it and in Bash, enclosing characters in single quotes means every character is being sent exactly as it is written.

How to fix this? The printf command was my eventual solution to the problem because in Bash, you can use printf to write a variable using what’s known as a format specifier. In this case, I needed to use the s format specifier because that will allow a string of characters to be used. To call the s format specifier for printf, %s% should be used.

To set it up, I needed to do the following in my script:

  1. Create the JSON string with %s% in place of ${asset_tag_number_goes_here} and store that JSON string as a variable.
  2. Use printf in a separate variable to create the JSON string and use the s format specifier to substitute the %s  with the value of the asset_tag_number_goes_here variable in the correct place inside the JSON string.
  3. Send the API command and include the JSON string created by printf.

The end result looks similar to this:


# Format a block of JSON and use %s to specify where printf should print a specified variable.
JSONBlockFormat='{"general":{"assetTag":"%s"}}'
# Use printf to create the needed JSON block and write the asset_tag_number_goes_here variable in the correct place in the JSON block.
JSONBlock=$(printf "$JSONBlockFormat" "$asset_tag_number_goes_here")
# Send an API command to update the computer inventory record with the specified asset tag information
/usr/bin/curl -s –header "Authorization: Bearer api_token_goes_here" "https://jamf.pro.server.here/api/v3/computers-inventory-detail/jamf_pro_id_goes_here&quot; -H "Content-Type: application/xml" -X PATCH -d "$JSONBlock"

文章来源: https://derflounder.wordpress.com/2025/12/22/using-printf-to-write-variable-values-to-json-strings-in-bash-scripts/
如有侵权请联系:admin#unsafe.sh