Yes, using the API is actually a better way to do it, since things like Buildings and Departments cannot be successfully added to a computer record using a recon unless the corresponding building or department exists, exact spelling included. So pulling them using the API avoids typing errors and also accounts for any changes and additions that can happen from the time the script is created.
Here's an example I pulled from one of my scripts to get the values of Buildings. You can adapt this for Departments. Just change the JSSResource URL and some of the wording and variables as appropriate and it should work. Note that the username and password for the API are passed to the script as script parameters $4 & $5 respectively.
#!/bin/bash
APIUSER="$4"
APIPASS="$5"
function getAPIValues ()
{
## Pull values from the enrolled to Jamf Pro server using the API
## Jamf Pro URL this Mac is enrolled to
JP_URL=$(/usr/bin/defaults read /Library/Preferences/com.jamfsoftware.jamf.plist jss_url | sed 's//$//')
## Get list of Buildings assigned in Jamf Pro
BUILDINGS=$(/usr/bin/curl -H "Accept: application/xml" -sku "${APIUSER}:${APIPASS}" "${JP_URL}/JSSResource/buildings" -X GET 2>/dev/null | xmllint --format - | awk -F'>|<' '/<name>/{print $3}')
BUILDING_SELECTION=$(/usr/bin/osascript << EOF
tell application "System Events"
set Builds to do shell script "echo "$BUILDINGS""
set BuildsList to paragraphs of Builds
set BUILDING_NAME to choose from list BuildsList with prompt "Choose a building for this computer." with title "Building Assignment"
end tell
EOF)
if [ "$BUILDING_SELECTION" == "false" ]; then
getAPIValues
else
echo "Building selected was: $BUILDING_SELECTION"
fi
}
## Call the function
getAPIValues