Skip to content

Bash jq JSON Parse Error Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Bash jq JSON Parse Error Fix. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Bash jq raises parse error when the input is not valid JSON, the file is empty, or the filter syntax does not match the JSON structure.

The Wrong Way

echo '{"name": "Alice", age: 30}' | jq '.name'

Output:

parse error: Invalid numeric literal at line 1, column 28

The JSON is invalid because age is not quoted.

The Right Way

echo '{"name": "Alice", "age": 30}' | jq '.name'

Output:

"Alice"

Ensure the input is valid JSON with proper quoting.

Step-by-Step Fix

1. Validate JSON before piping

echo '{"name": "Alice"}' | jq '.'

2. Check for empty or malformed input

if [ -s file.json ]; then
  jq '.' file.json
else
  echo "File is empty"
fi

3. Use raw output for string values

jq -r '.name' data.json  # removes quotes from string output

4. Handle nested objects with correct syntax

jq '.user.address.city' data.json
jq '.users[] | select(.active == true)' data.json

5. Use --arg for variable interpolation

key="name"
jq --arg k "$key" '.[$k]' data.json

Prevention Tips

  • Validate JSON with jq '.' before applying complex filters.
  • Use jq -r for raw string output without quotes.
  • Quote JSON keys and string values in the input.
  • Use jq -e to check if a filter matched anything (exit code 0/1).
  • Use jq --arg to pass shell variables into jq filters.

Common Mistakes with jq parse

  1. Mixing let bindings with <- bindings in do notation, producing type errors
  2. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  3. Non-exhaustive pattern matches that compile with warnings then crash at runtime

These mistakes appear frequently in real-world BASH code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does jq say "parse error: Unexpected string"?

This usually means the JSON has unquoted string values. In JSON, all strings must be in double quotes. Use a JSON linter (jq '.') to find syntax errors.

How do I pretty-print JSON with jq?

jq '.' pretty-prints valid JSON. Use jq -c '.' for compact output. Use jq --tab '.' for tab-indented output.

How do I access an array element in jq?

Use jq '.[0]' for the first element, jq '.[-1]' for the last element, and jq '.[:3]' for slicing. Combined with keys: jq '.users[0].name'.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro