有没有可以产生人类可读的JSON输出的golang的jq包装器? [英] Is there a jq wrapper for golang that can produce human readable JSON output?
问题描述
我正在编写一个在标准输出上输出JSON的go程序(我们称之为foo)。
$ ./foo
{id:uuid1,name:John Smith} {id:uuid2,name:Jane Smith}
使输出成为人类可读的,我必须将其输入到jq中:
$ ./foo | jq。
{
id:uuid1,
name:John Smith
}
{
id :uuid2
name:Jane Smith
}
有没有办法使用开源的jq包装来达到相同的结果?我试图找到一些,但他们通常包装过滤JSON输入的功能,而不是美化JSON输出。 输出它在 Go Playground 上): encoding / json
软件包支持漂亮的输出-THE盒。您可以使用 json.MarshalIndent()
。或者,如果您使用 json.Encoder
,然后调用它的 Encoder.SetIndent( )
(自 Go 1.7 以来的新增内容)方法调用 Encoder.Encode()$ c
$ b $ := map [string] interface {} {id:uuid1,name:John Smith}
data,err:= json.MarshalIndent(m,, )
if err!= nil {
panic(err)
}
fmt.Println(string(data))
enc:= json .NewEncoder(os.Stdout)
enc.SetIndent(,)
if err:= enc.Encode(m); err!= nil {
panic(err)
}
{
id:uuid1,
name:John Smith
}
{
id:uuid1,
name:John Smith
}
如果您只想格式化准备好的JSON文本,则可以使用 json.Indent()
函数:
src: =`{id:uuid1,name:John Smith}`
dst:=&; bytes.Buffer {}
如果err:= json.Indent (dst,[] byte(src),,); err!= nil {
panic(err)
}
fmt.Println(dst.String())
输出(在去游乐场试试) :
{
id:uuid1,
name:John Smith
2 字符串
这些 indent
函数的参数是:
前缀,缩进字符串
解释在文档中:
JSON对象或数组中的每个元素都从一个以prefix开头的新缩进行开始,后跟一个或多个indentation嵌套缩进副本。
所以每一个换行符都会以前缀
开头,后面跟着0个或更多个 indent
,这取决于嵌套级别。
如果您指定val他们是这样的:
json.Indent(dst,[] byte(src),+, - )
使用嵌入对象进行测试:
src:=`{id:uuid1,name:John Smith,embedded::{fieldx:y}}`
dst:=&; bytes.Buffer {}
if err:= json.Indent(dst,[] byte(src),+, - ); err!= nil {
panic(err)
}
fmt.Println(dst.String())
输出(在去游乐场试试) :
{
+ - id:uuid1,
+ - name: John Smith,
+ - embedded::{
+ - fieldx:y
+ - }
+}
I'm writing a go program (let's call it foo) that outputs JSON on the Standard Out.
$ ./foo
{"id":"uuid1","name":"John Smith"}{"id":"uuid2","name":"Jane Smith"}
In order to make the output human readable, I have to pipe it into jq like:
$ ./foo | jq .
{
"id":"uuid1",
"name": "John Smith"
}
{
"id":"uuid2"
"name": "Jane Smith"
}
Is there a way to achieve the same result using a jq wrapper that is open sourced? I tried finding some but they are usually wrapping the functionality for filtering JSON input not prettifying JSON output.
The encoding/json
package supports pretty output out-of-the-box. You may use json.MarshalIndent()
. Or if you're using json.Encoder
, then call its Encoder.SetIndent()
(new since Go 1.7) method prior to calling Encoder.Encode()
.
Examples:
m := map[string]interface{}{"id": "uuid1", "name": "John Smith"}
data, err := json.MarshalIndent(m, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(data))
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
if err := enc.Encode(m); err != nil {
panic(err)
}
Output (try it on the Go Playground):
{
"id": "uuid1",
"name": "John Smith"
}
{
"id": "uuid1",
"name": "John Smith"
}
If you just want to format a "ready" JSON text, you may use the json.Indent()
function:
src := `{"id":"uuid1","name":"John Smith"}`
dst := &bytes.Buffer{}
if err := json.Indent(dst, []byte(src), "", " "); err != nil {
panic(err)
}
fmt.Println(dst.String())
Output (try it on the Go Playground):
{
"id": "uuid1",
"name": "John Smith"
}
The 2 string
parameters to these indent
functions are:
prefix, indent string
Explanation is in the docs:
Each element in a JSON object or array begins on a new, indented line beginning with prefix followed by one or more copies of indent according to the indentation nesting.
So each newline will be started with prefix
, which will be followed by 0 or more copies of indent
, depending on the nesting level.
It becomes clear and obvious if you specify values for them like this:
json.Indent(dst, []byte(src), "+", "-")
Testing it with embedded objects:
src := `{"id":"uuid1","name":"John Smith","embedded:":{"fieldx":"y"}}`
dst := &bytes.Buffer{}
if err := json.Indent(dst, []byte(src), "+", "-"); err != nil {
panic(err)
}
fmt.Println(dst.String())
Output (try it on the Go Playground):
{
+-"id": "uuid1",
+-"name": "John Smith",
+-"embedded:": {
+--"fieldx": "y"
+-}
+}
这篇关于有没有可以产生人类可读的JSON输出的golang的jq包装器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!