CodexBloom - Programming Q&A Platform

Bash script fails to parse JSON output from `curl` when using `jq` with special characters

👀 Views: 0 💬 Answers: 1 📅 Created: 2025-09-01
bash curl jq json Bash

I've searched everywhere and can't find a clear answer. This might be a silly question, but I'm experimenting with Hey everyone, I'm running into an issue that's driving me crazy... I'm working on a project and hit a roadblock... I've been banging my head against this for hours. I'm trying to use a Bash script to fetch JSON data from an API endpoint and parse it using `jq`. The API returns various fields, including some that contain special characters. However, whenever the returned JSON contains these characters, I encounter parsing errors. For example, the output from `curl` looks like this: ```json { "name": "John Doe", "bio": "Loves coding & coffee!" } ``` When I run the following command: ```bash curl -s https://api.example.com/user | jq '.bio' ``` I get the error: ``` jq: error: Expected value but found ‘&’ at line 1, column 11 ``` I’ve tried using `printf` to escape special characters, but that didn't resolve the issue. I've also tested the JSON directly in `jq` using: ```bash echo '{"name":"John Doe", "bio":"Loves coding & coffee!"}' | jq '.bio' ``` This works without any issues. It seems like the special characters in the output from `curl` are causing problems. I’m using `jq` version 1.6 and Bash 5.0. I would appreciate any guidance on how to properly handle or escape special characters in the JSON output from `curl` so that `jq` can parse it correctly. Is there a best practice for this scenario? For context: I'm using Bash on macOS. Any help would be greatly appreciated! I'd really appreciate any guidance on this. My development environment is Linux. Has anyone else encountered this? How would you solve this? Is there a better approach? My team is using Bash for this application. Any examples would be super helpful.