Skip to content

Instantly share code, notes, and snippets.

@iandol
Last active April 13, 2026 09:06
Show Gist options
  • Select an option

  • Save iandol/fb6ad60030e3ffcb07bdee4a5aa3557b to your computer and use it in GitHub Desktop.

Select an option

Save iandol/fb6ad60030e3ffcb07bdee4a5aa3557b to your computer and use it in GitHub Desktop.
Uses BetterBibTeX JSON-RPC API to search Zotero from command line. You can call it like this: `zoteroSearch '[["itemID", "is", "12187"]]' | jq '.result[0].citekey'` which will search for itemID and return just the citekey
#!/bin/zsh
# Check if an argument was provided
if [[ -z "$1" ]]; then
echo "Usage: $0 '[\"creator\", \"contains\", \"Doe\"]'"
exit 1
fi
# The search terms passed as the first argument
# Example: '["creator", "contains", "Johnny"]'
SEARCH_TERMS=$1
# Better BibTeX JSON-RPC endpoint
RPC_URL="http://localhost:23119/better-bibtex/json-rpc"
# Use jq to ensure the input is treated as an array of arrays
# This handles both ["a","b","c"] and [["a","b","c"]] correctly for the API
PAYLOAD=$(jq -n --argjson terms "$SEARCH_TERMS" '{
jsonrpc: "2.0",
method: "item.search",
params: [ (if ($terms[0] | type) == "string" then [$terms] else $terms end) ],
id: 1
}')
# Check if jq actually produced valid JSON
if [[ -z "$PAYLOAD" ]]; then
echo "Error: The search term provided is not valid JSON."
echo "Hint: Use double quotes for strings: '[\"creator\", \"contains\", \"name\"]'"
exit 1
fi
# 2. Perform the request and pipe directly through a cleaner into jq
# 'tr -d "\000-\037"' removes control characters that break the JSON spec
curl -s -X POST "$RPC_URL" \
-H "Content-Type: application/json" \
-d "$PAYLOAD" \
| tr -d '\000-\010\013\014\016-\037' \
| jq .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment