zed icon indicating copy to clipboard operation
zed copied to clipboard

Formatted ("pretty print") JSON output

Open hboutou opened this issue 2 years ago • 2 comments

The -z gives non-formatted output. For "pretty output", there is -Z

zq -z '{"foo":1, "bar":2}'
{foo:1,bar:2}

zq -Z '{"foo":1, "bar":2}'
{
    foo: 1,
    bar: 2
}

However, there is no JSON equivalent for -Z

zq -j '{"foo":1, "bar":2}'
{"foo":1,"bar":2}

zq -J '{"foo":1, "bar":2}'
flag provided but not defined: -J

For pretty output, i have to opt to secondary tool

zq -j '{"foo":1, "bar":2}' | jq
{
  "foo": 1,
  "bar": 2
}

Is there a way to get JSON formatted output without reaching to a another tool ?

hboutou avatar Mar 05 '24 19:03 hboutou

@hboutou: Thanks for your interest in Zed!

There's not currently a way to get the output you're seeking directly from the Zed CLI tools, but given that -Z exists as you pointed out and how common JSON is, I totally understand why you'd hoped to see it there. I'll take up the topic with the Dev team to see the feasibility of adding this soon and will circle back with an update to this issue.

philrz avatar Mar 12 '24 01:03 philrz

We discussed this one today as a group. There was consensus that this would indeed be a handy enhancement and probably not too difficult to add. It was pointed out that getting the colored output similar to jq we might mean leveraging JSON libraries other than the standard Go ones, hence maybe looking at projects like https://github.com/itchyny/gojq and https://github.com/TylerBrock/colorjson.

philrz avatar Mar 12 '24 17:03 philrz

Verified in Zed commit 20a867d.

The new -J option now pretty prints color JSON output.

image

@hboutou: If you're ok compiling Zed from the tip of main you can take advantage of this right away. Otherwise, it'll be in the next GA Zed release which is expected in the next couple weeks.

Thanks @mattnibs!

philrz avatar Apr 16 '24 17:04 philrz

Compiled and tested, works like a charm, thank you!

hboutou avatar Apr 17 '24 07:04 hboutou