gpt4 book ai didi

jq - 如何修改jq中数组的每个元素

转载 作者:行者123 更新时间:2023-12-02 17:56:54 25 4
gpt4 key购买 nike

假设我有一个 JSON:

[
{
"title": "Title1",
"reference": [
"123"
]
},
{
"title": "Title2",
"reference": [
"234",
"345"
]
}
]

我想修改引用数组的每个元素,以便引用出现两次。我想要实现的目标:

[
{
"title": "Title1",
"reference": [
"123 is 123"
]
},
{
"title": "Title2",
"reference": [
"234 is 234",
"345 is 345"
]
}
]

我已经尝试过:

jq '.[] | .reference = [("\(.reference[]) is \(.reference[])")]'

但是当数组有多个项目时,这会失败:

{
"title": "Title1",
"reference": [
"123 is 123"
]
}
{
"title": "Title2",
"reference": [
"234 is 234",
"345 is 234",
"234 is 345",
"345 is 345"
]
}

如何修改上面的jq以达到预期的结果?

提前致谢!

最佳答案

map(.reference |= map(. + " is " + .))

将每个 .reference 更改为 .reference is .reference


[
{
"title": "Title1",
"reference": [
"123 is 123"
]
},
{
"title": "Title2",
"reference": [
"234 is 234",
"345 is 345"
]
}
]

Demo

关于jq - 如何修改jq中数组的每个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75461609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com