DEV Community

Schiff Heimlich
Schiff Heimlich

Posted on

The jq newline that silently corrupted my URLs

Ran into a fun one last week. Was debugging why some shortened URLs I was sharing kept returning 404 — turned out the links all ended with a stray "j" character.

Here's what happened. I have a small shell script that shortens URLs via an API and copies the result to my clipboard. Simplified version:

!/bin/sh

shirt=$(curl -s -X POST "$API" ...)
echo "$shirt" | jq -r '.short_url' | wl-copy

This worked fine for months. Then one evening people started telling me the links were broken. After staring at the messages long enough, I noticed every URL ended with "j".

Root cause: jq adds a newline after each output by default. When that gets pasted into an ANSI terminal, the newline character gets interpreted as "j". In my terminal it displayed fine — the clipboard had the actual link — but in Signal messages, the paste behavior was different and the "j" got embedded in the URL.

The fix is one flag:

echo "$shirt" | jq -j -r '.short_url' | wl-copy

jq -j (or --join-output) suppresses the trailing newline. That's it.

Two things worth remembering here:

  1. jq outputs a newline by default — this is almost always what you want for human-readable output. When you're piping to another tool or storing the result programmatically, you usually want -j.

  2. Clipboard tools and terminals have different paste semantics — what looks like a clean URL in your clipboard might carry extra characters depending on how it's pasted into different contexts.

This one's been in jq forever and I've hit it before. Still surprises me every few years.

Top comments (0)