json 从单行获取 jq 的输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40396445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:41:09  来源:igfitidea点击:

Get outputs from jq on a single line

jsonjq

提问by Jitesh Sojitra

I got below output using: https://stackoverflow.com/a/40330344

我使用以下输出:https: //stackoverflow.com/a/40330344

 (.issues[] | {key, status: .fields.status.name, assignee: .fields.assignee.emailAddress})

Output:

输出:

 {  
   "key": "SEA-739",
   "status": "Open",
   "assignee": null
 }
 {
   "key": "SEA-738",
   "status": "Resolved",
   "assignee": "[email protected]"
 }

But I need to parse each and every line but it's tough to identify which assignee is for which key as far as key group is concerned. Is this possible to make one bunch in one row using jq?

但是我需要解析每一行,但是就关键组而言,很难确定哪个受让人是哪个关键的。是否可以使用 jq 在一行中制作一堆?

Expected output:

预期输出:

{ "key": "SEA-739", "status": "Open", "assignee": null }
{ "key": "SEA-738", "status": "Resolved", "assignee": "[email protected]"}

OR

或者

{ "SEA-739", "Open", null }
{ "SEA-738", "Resolved", [email protected] }

回答by hmedia1

-cis what you likely need

-c是你可能需要的

Using the output you posted above, you can process it further:

使用您在上面发布的输出,您可以进一步处理它:

jq -c . input

To Give;

给予;

{"key":"SEA-739","status":"Open","assignee":null}
{"key":"SEA-738","status":"Resolved","assignee":"[email protected]"}

Or you can just change your original command

或者您可以更改原始命令

FROM

jq -r '(.issues[] | {key, status: .fields.status.name, assignee: .fields.assignee.emailAddress})'

TO

jq -c '(.issues[] | {key, status: .fields.status.name, assignee: .fields.assignee.emailAddress})'

回答by bschlueter

Not precisely an answer to the long version of the question, but for people who Googled this looking for other single line output formats from jq:

不完全是对长版问题的回答,但对于那些在谷歌上搜索 jq 的其他单行输出格式的人来说:

$ jq -r '[.key, .status, .assignee]|@tsv' <<<'
 {
   "key": "SEA-739",
   "status": "Open",
   "assignee": null
 }
 {
   "key": "SEA-738",
   "status": "Resolved",
   "assignee": "[email protected]"
 }'
SEA-739 Open
SEA-738 Resolved        [email protected]

@shrather than @tsvreturns:

@sh而不是@tsv回报:

'SEA-739' 'Open' null
'SEA-738' 'Resolved' '[email protected]'

Additionally, there are other output formats to do things such as escape the output, like @html, or encode it, as with @base64. The list is available in Format strings and escapingsection of either the jq(1)man page or online at stedolan.github.io/jq/manual.

此外,还有其他输出格式可以执行诸如对输出进行转义(例如@html)或对其进行编码(如@base64. 该列表可在jq(1)手册页的格式字符串和转义部分或在stedolan.github.io/jq/manual在线获得