需要明确的是 - 我想要一个伪 JSON 输出,该输出将被写入此处的另一个静态 JSON 文件中...因此不包含在数组或任何内容中,只需在输出中的每个实体后面获取丢失的逗号即可。
目前我的查询是:
.[] | select(.auditId == "categories") |
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*\(.auditProperty):* \(.actual) (expected \(.expected))"
}
}
哪个输出:
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*performance:* 1 (expected 0.8)"
}
}
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*accessibility:* 1 (expected 0.9)"
}
}
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*best-practices:* 0.96 (expected 0.9)"
}
}
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*seo:* 0.64 (expected 0.5)"
}
}
当我真正想要的时候:
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*performance:* 1 (expected 0.8)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*accessibility:* 1 (expected 0.9)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*best-practices:* 0.96 (expected 0.9)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*seo:* 0.64 (expected 0.5)"
}
},
请注意每个实体后面的逗号!这让我发疯,我尝试join(", ")
在不同的地方添加 a ,但这要么对最终输出没有任何作用,要么无法编译,具体取决于我放置它的位置。
这是包含数据的jqplayhttps://jqplay.org/s/xx3F_IWn03g
原始输入 JSON:
[
{
"name": "minScore",
"expected": 0.8,
"actual": 1,
"values": [
1,
1,
1
],
"operator": ">=",
"passed": true,
"auditProperty": "performance",
"auditId": "categories",
"level": "error",
"url": "http://localhost:8080/page2"
},
{
"name": "minScore",
"expected": 0.9,
"actual": 1,
"values": [
1,
1,
1
],
"operator": ">=",
"passed": true,
"auditProperty": "accessibility",
"auditId": "categories",
"level": "error",
"url": "http://localhost:8080/page2"
},
{
"name": "minScore",
"expected": 0.9,
"actual": 0.96,
"values": [
0.93,
0.96,
0.96
],
"operator": ">=",
"passed": true,
"auditProperty": "best-practices",
"auditId": "categories",
"level": "error",
"url": "http://localhost:8080/page2"
},
{
"name": "minScore",
"expected": 0.5,
"actual": 0.64,
"values": [
0.64,
0.64,
0.64
],
"operator": ">=",
"passed": true,
"auditProperty": "seo",
"auditId": "categories",
"level": "error",
"url": "http://localhost:8080/page2"
}
]
答案1
你似乎想要调整auditId
已设置为 string 的条目categories
。你正在做的是提炼顶级数组中的这些元素并修改它们。这为您提供了一组(而不是数组)对象。
|
不要使用从数组中提取元素,而是使用|=
修改其元素。请参阅此答案的末尾,了解如何将结果数组正确插入到另一个文件中的 JSON 结构中。
所以,使用
.[] |= (
select(.auditId == "categories") |
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*\(.auditProperty):* \(.actual) (expected \(.expected))"
}
}
)
或者,根本不使用.[]
而是使用map()
顶级数组中每个元素的表达式:
map(
select(.auditId == "categories") |
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*\(.auditProperty):* \(.actual) (expected \(.expected))"
}
}
)
这些表达式将为您提供一个如下所示的元素数组:
[
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*performance:* 1 (expected 0.8)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*accessibility:* 1 (expected 0.9)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*best-practices:* 0.96 (expected 0.9)"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*seo:* 0.64 (expected 0.5)"
}
}
]
array
要将此数组添加到另一个文件中已存在的数组中,您可以执行以下操作(假设的 顶层有一个键otherfile
,并且您的输入位于 中inputfile
;如果该键尚不存在,则将创建该键):
jq '.array += ( input |
map(
select(.auditId == "categories") |
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*\(.auditProperty):* \(.actual) (expected \(.expected))"
}
}
)
)' otherfile inputfile
执行时input
,将从命令行上的第二个文件读取数组。
如果您想替换现有键的值而不是添加到现有键的值,请更改+=
为 just 。=
array
答案2
是的,这就是[]
迭代器的作用,每个迭代器都会获得一个 JSON 文件。要获取数组,您可以将整个表达式括起来[...]
以从中创建一个数组,或者使用map()
它就地修改输入数组,而不是[]
迭代其元素。
$ echo '[1,2,3,4]' | jq '.[]|select(.<3)'
1
2
$ echo '[1,2,3,4]' | jq '[.[]|select(.<3)]'
[
1,
2
]
$ echo '[1,2,3,4]' | jq 'map(select(.<3))'
[
1,
2
]
使用逗号分隔的元素而不包含[
,]
使其成为数组会使其无效 json,但如果这是您想要的,您可以通过管道将其sed '1d;$d'
删除第一行和最后一行。