Getting parse error with jq when processing OpenAI API response

I’m a beginner programmer working on a bash script that translates clipboard text using OpenAI’s API. The script copies selected text and sends it to OpenAI for translation, but I’m running into a parsing issue.

Here’s my current code:

xdotool key ctrl+a
xdotool key ctrl+c
export OPENAI_API_KEY='YYYYYYYYYYYYYYYYYYYY'
result=$(openai api completions.create -m text-davinci-003 -p 'Convert "$(xclip -o -sel c)" to English.' | jq -r '.choices[0].text')

When I run echo "$result", I keep getting this error: “parse error: Invalid numeric literal at line 1, column 10”

I think the issue might be related to quote handling or the JSON format returned by OpenAI, but I’m not sure how to debug this properly. Has anyone encountered similar problems when parsing OpenAI API responses with jq?

I hit this exact issue when I started using OpenAI’s API with bash. Your $(xclip -o -sel c) command substitution probably isn’t executing right - it’s sending the literal string to OpenAI instead of your actual clipboard content. This messes up the API response and breaks jq parsing. Store your clipboard content in a variable first, then use that in your prompt. Also, pipe the raw API response to a file before running it through jq so you can see what you’re actually getting back. OpenAI sometimes returns plain text error messages instead of JSON when requests go wrong.

Your command substitution isn’t working because bash can’t expand $(xclip -o -sel c) properly when it’s nested in those quotes. OpenAI’s getting the literal text $(xclip -o -sel c) instead of your actual clipboard contents, which breaks the API call and gives you malformed responses that crash jq. I’ve hit this same issue building automation scripts. Grab your clipboard first with clipboard_text=$(xclip -o -sel c) then use $clipboard_text in your prompt. Don’t forget to escape special characters in the clipboard that might mess up JSON parsing. That numeric literal error means you’re getting back something starting with a broken number - usually happens when the API returns an error instead of the expected completion.

First, check your OpenAI CLI version - older versions had different output formats that break jq parsing. Try adding --stream=false to your OpenAI command too, since streaming responses can cause weird parse errors. That numeric literal error usually means jq found a malformed number somewhere in the JSON.

That numeric literal error usually means you’re getting malformed JSON back. The real issue is probably that your OpenAI CLI command is crashing and spitting out an error message instead of actual JSON. I’ve hit this before when my API key wasn’t set up right or when there were unescaped characters in the prompt. Run the openai command by itself first (without the jq part) so you can see what’s actually coming back. Also, that command substitution with all the nested quotes looks messy - those inner quotes are probably messing up how your prompt gets built. I’d create the prompt string separately first, then pass it as a variable. Way easier than dealing with quote hell.