英文:
How to use jq to create a JSON object
问题
{
"abc": {
"name": "abc"
},
"xyz": {
"name": "xyz"
}
}
英文:
I have the following JSON object:
{
...,
"projects": [
"abc",
"xyz"
],
...
}
And I want it transformed to:
{
"abc": {
"name": "abc"
},
"xyz": {
"name": "xyz"
},
}
I'm having trouble creating this. I've tried using map
as .projects | map({(.): { "name": (.) }} )
but it's not in the format I want and ends up in an array.
答案1
得分: 2
你在寻找这样的内容:
.projects | map({(.): {name: .}}) | add
英文:
You're looking for something like this:
.projects | map({(.): {name: .}}) | add
<sup>Online demo</sup>
答案2
得分: 1
我有一种功能性思维,所以我会在这里使用 reduce
函数:
reduce .projects[] as $name ({}; . + {($name): {name: $name}})
输出结果:
{
"abc": {
"name": "abc"
},
"xyz": {
"name": "xyz"
}
}
英文:
I have a bit of a functional mindset, so I'd reach for reduce
here:
reduce .projects[] as $name ({}; . + {($name): {name: $name}})
outputs
{
"abc": {
"name": "abc"
},
"xyz": {
"name": "xyz"
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论