I did find a solution while I was writing the question: don't put the input inside string interpolation, output a stream of things:

echo '{"foo":"bar", "baz":[1,2,3]}' | jq -r '"My value is:", . , "Some other stuff"'
# .........................................................^^^^^

outputs

My value is:
{
  "foo": "bar",
  "baz": [
    1,
    2,
    3
  ]
}
Some other stuff
Answer from glenn jackman on Stack Overflow
🌐
LornaJane
lornajane.net › posts › 2024 › pretty-print-json-with-jq
Pretty-print JSON with jq | LornaJane
cat is a command to print the contents of the file to stdout. It’s a good way to get content into the start of a chain of commands. The | operator sends the output of the cat command into the next thing in the command. jq is a JSON tool, and the "." part is technically a filter – but it includes everything, so it’s more like a not-filter. The output of jq is the same JSON that we put in, but it’s pretty-printed.
Discussions

How to pretty print a JSON inside a string with jq? - Stack Overflow
I'm writing a more complex transformation with jq, and one of the things I'd like to do is to have a pretty printed JSON inside a string. More on stackoverflow.com
🌐 stackoverflow.com
request: make jq stdin accept json and output pretty print json
it's very common to use jq to pretty print json. would it hurt to have jq accept stdin standard and output as if it had been called as jq .[] More on github.com
🌐 github.com
2
January 17, 2017
No pretty print by default on JSON, compared to jq and python yq
When you run the python version of yq, the default is to pretty print JSON. Like jq too. Related: https://stackoverflow.com/a/27238477/465183 Version of yq: 4.30.5 Operating system: Linux Mint Inst... More on github.com
🌐 github.com
5
February 22, 2023
unix - How can I pretty-print JSON in a shell script? - Stack Overflow
Great formatting tool, just one ... pretty saves a file with color formatting signs being inserted, smth like: [32m, [33m, [39m along with some non-printable before each of them, which makes JSON not valid. However, underscore print alone doesn't add anything to a file and does its formatting job perfectly. 2015-01-24T12:18:57.587Z+00:00 ... I love jq but this worked ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
jq
jqlang.org › manual
jq 1.8 Manual
Instead of running the filter for each JSON object in the input, read the entire input stream into a large array and run the filter just once. ... By default, jq pretty-prints JSON output.
🌐
Discoposse
discoposse.com › home › using jq to pretty print json output
Using jq to pretty print JSON output - DiscoPosse.com
November 12, 2017 - William has posted JSON content for the VMworld session content from the US event at his Github page: Let’s click the Raw button on the page to render the real content URL which we will consume: It’s not too readable in the browser, or the command line as you can see when we run the cURL command: All we have to do to fix that up is to pipe the output from our cURL command to jq and we are able to see the pretty printed version of the JSON:
🌐
Nick Janetakis
nickjanetakis.com › blog › pretty-print-json-in-your-terminal-with-jq-or-python
Pretty Print JSON in Your Terminal with jq or Python — Nick Janetakis
July 4, 2023 - jq < data.json jq --indent 4 < data.json # Replace `xclip -out -selection clipboard` with `pbpaste` if you're on macOS xclip -out -selection clipboard | jq xclip -out -selection clipboard | jq --indent 4 · I created a small ppjson script which is available in my dotfiles. It requires having jq installed and it will either use xclip or pbpaste depending on what OS you have. ... # Pretty print the contents of your clipboard with 2 spaces of indentation ppjson # Pretty print the content of a file ppjson data.json
🌐
Kyle Howells Blog
ikyle.me › blog › 2024 › pretty-print-json-terminal
Pretty Print JSON in the Terminal - Kyle Howells
August 8, 2024 - This will print the JSON response in a readable, pretty-printed format. jq is actually meant not to pretty print json, but to transform and manipulate json data. The . argument to it tells it to return the input unchanged as the output.
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-transform-json-data-with-jq
How To Transform JSON Data with jq | DigitalOcean
September 23, 2025 - By default, jq will pretty print its output. It will automatically apply indentation, add new lines after every value, and color its output when possible. Coloring may improve readability, which can help many developers as they examine JSON data produced by other tools.
Top answer
1 of 2
4

This:

echo '{"foo": "bar"}' | jq '{other: .}' | jq -Rs '{json: .}'

produces:

{
  "json": "{\n  \"other\": {\n    \"foo\": \"bar\"\n  }\n}\n"
}

One way to remove the terminating "\n" would be to strip it:

echo '{"foo": "bar"}' | jq '{other: .}' | jq -Rs '{json: .[:-1]}'
2 of 2
4

I ended up writing a simple formatting function:

#   9 = \t
#  10 = \n
#  13 = \r
#  32 = (space)
#  34 = "
#  44 = ,
#  58 = :
#  91 = [
#  92 = \
#  93 = ]
# 123 = {
# 125 = }

def pretty:
  explode | reduce .[] as $char (
    {out: [], indent: [], string: false, escape: false};
    if .string == true then
      .out += [$char]
      | if $char == 34 and .escape == false then .string = false else . end
      | if $char == 92 and .escape == false then .escape = true else .escape = false end
    elif $char == 91 or $char == 123 then
      .indent += [32, 32] | .out += [$char, 10] + .indent
    elif $char == 93 or $char == 125 then
      .indent = .indent[2:] | .out += [10] + .indent + [$char]
    elif $char == 34 then
      .out += [$char] | .string = true
    elif $char == 58 then
      .out += [$char, 32]
    elif $char == 44 then
      .out += [$char, 10] + .indent
    elif $char == 9 or $char == 10 or $char == 13 or $char == 32 then
      .
    else
      .out += [$char]
    end
  ) | .out | implode;

It adds unnecessary empty lines inside empty objects and arrays, but it's good enough for my purpose. For example (used on its own):

jq -Rr 'include "pretty"; pretty' test.json

where the function is saved in pretty.jq and test.json file is:

{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"key":"string with \"quotes\" and \\"},"geometry":{"type":"Polygon","coordinates":[[[24.2578125,55.178867663281984],[22.67578125,50.958426723359935],[28.125,50.62507306341435],[30.322265625000004,53.80065082633023],[24.2578125,55.178867663281984]]]}}]}

gives:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "key": "string with \"quotes\" and \\"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              24.2578125,
              55.178867663281984
            ],
            [
              22.67578125,
              50.958426723359935
            ],
            [
              28.125,
              50.62507306341435
            ],
            [
              30.322265625000004,
              53.80065082633023
            ],
            [
              24.2578125,
              55.178867663281984
            ]
          ]
        ]
      }
    }
  ]
}
🌐
Shapeshed
shapeshed.com › jq-json
JSON on the command line with jq | George Ornbo
September 19, 2024 - [{ "id": 1, "name": "Arthur", "age": "21" },{ "id": 2, "name": "Richard", "age": "32" }] jq can pretty print this file using the . filter. This takes the entire input and sends it to standard output.
🌐
Today I Learned
til.hashrocket.com › posts › uvsd8l7zes-pretty-print-json-in-neovimvim-using-jq
Pretty-Print JSON in NeoVim/Vim using jq - Today I Learned
August 30, 2024 - curl https://til.hashrocket.com/api/developer_posts.json?username=doriankarter | jq · You can also use jq inside of NeoVim to pretty print a JSON string, right in your buffer using this command:
🌐
Baeldung
baeldung.com › home › web › guide to linux jq command for json processing
Guide to Linux jq Command for JSON Processing | Baeldung on Linux
August 31, 2025 - We echo a simple JSON string and pipe it into our jq command. Then we use the identity filter ‘.’ that takes the input and produces it unchanged as output with the caveat that by default jq pretty-prints all output.
🌐
JSONLint
jsonlint.com › json-pretty-print
JSON Pretty Print - Format & Beautify JSON Online | JSONLint | JSONLint
# Pretty print a file cat data.json | jq '.' # Pretty print with sorted keys cat data.json | jq -S '.'
🌐
GitHub
github.com › jqlang › jq › issues › 1310
request: make jq stdin accept json and output pretty print json · Issue #1310 · jqlang/jq
January 17, 2017 - it's very common to use jq to pretty print json. would it hurt to have jq accept stdin standard and output as if it had been called as jq .[]
Author   conrado
🌐
GitHub
github.com › mikefarah › yq › issues › 1568
No pretty print by default on JSON, compared to jq and python yq · Issue #1568 · mikefarah/yq
February 22, 2023 - When you run the python version of yq, the default is to pretty print JSON. Like jq too. Related: https://stackoverflow.com/a/27238477/465183 Version of yq: 4.30.5 Operating system: Linux Mint Installed via: go Input JSON {"type":"Bar","...
Author   sputnick-dev
🌐
YouTube
youtube.com › nick janetakis
Pretty Print JSON in Your Terminal with jq or Python - YouTube
We'll also cover creating a script to read a file or your clipboard, complete with syntax highlighting.Hit the subscribe button to receive more videos like t...
Published   July 4, 2023
Views   892
🌐
Bessarabov
ivan.bessarabov.com › blog › pretty-json-with-jq
JSON pretty print with jq
It is not visible in the text, but jq uses color to output json.
🌐
Linode
linode.com › docs › guides › using-jq-to-process-json-on-the-command-line
How to Use JQ to Process JSON on the Command Line | Linode Docs
November 5, 2021 - This operation takes a JSON file and formats it into easy-to-read output, with proper line spacing, standard indentation, and perfectly aligned braces. To prettify a JSON file, use the jq '.' command. The . symbol is known as the identity command.
🌐
Ucdavis
dw-test.elc.ucdavis.edu › home › jampu
Jq Pretty Print Section - Data Science Workbench
October 18, 2023 - Welcome to this in-depth guide on using the jq command for pretty printing JSON data. JSON (JavaScript Object Notation) is a widely used data interchange format, and jq is a powerful tool for processing and manipulating it.
Top answer
1 of 16
5228

With Python 2.6+ or 3 you can use the json.tool module:

echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool

or, if the JSON is in a file, you can do:

python -m json.tool my_json.json

if the JSON is from an internet source such as an API, you can use

curl http://my_url/ | python -m json.tool

For convenience in all of these cases you can make an alias:

alias prettyjson='python -m json.tool'

For even more convenience with a bit more typing to get it ready:

prettyjson_s() {
    echo "$1" | python -m json.tool
}

prettyjson_f() {
    python -m json.tool "$1"
}

prettyjson_w() {
    curl "$1" | python -m json.tool
}

for all the above cases. You can put this in .bashrc and it will be available every time in shell. Invoke it like prettyjson_s '{"foo": "lorem", "bar": "ipsum"}'.

Note that as @pnd pointed out in the comments below, in Python 3.5+ the JSON object is no longer sorted by default. To sort, add the --sort-keys flag to the end. I.e. ... | python -m json.tool --sort-keys.

Another useful option might be --no-ensure-ascii which disables escaping of non-ASCII characters (new in version 3.9).

2 of 16
1657

You can use: jq

It's very simple to use and it works great! It can handle very large JSON structures, including streams. You can find their tutorials here.

Usage examples:

$ jq --color-output . file1.json file1.json | less -R

$ command_with_json_output | jq .

$ jq # stdin/"interactive" mode, just enter some JSON

$ jq <<< '{ "foo": "lorem", "bar": "ipsum" }'
{
  "bar": "ipsum",
  "foo": "lorem"
}

Or use jq with identity filter:

$ jq '.foo' <<< '{ "foo": "lorem", "bar": "ipsum" }'
"lorem"