Home > Jamf Pro API, Jamf Pro Classic API, Scripting > 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
As part of that command, the following XML string is being sent with the asset_tag_number_goes_here variable being included:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "<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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
My JSON string looked like this:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '{"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:
The end result looks similar to this:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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" -H "Content-Type: application/xml" -X PATCH -d "$JSONBlock" |