英文:
Create object starting from a list of string with jq
问题
我理解你的请求,以下是翻译好的部分:
{
"nonprod/global": [
"nonprod/global/app/sit01",
"nonprod/global/app/dev02",
"nonprod/global/app/dev03"
],
"prod/eu": [
"prod/eu/app/prod01"
],
"prod/na": [
"prod/na/app/prod01"
],
"test/eu": [
"test/eu/app/cust01"
]
}
如果你有其他翻译需求,请告诉我。
英文:
I would like to create a json object starting with a list of string in json format as the following
[
"nonprod/global/app/sit01",
"nonprod/global/app/dev02",
"prod/na/app/prod01",
"test/eu/app/cust01",
"nonprod/global/app/dev03"
]
What I would like to obtain is a map of lists of strings which would have as key the first two words of the string from the list and as value the strings that starts with that specific key.
I've tried the following:
jq 'reduce (.[] | capture("(?<env_type>([^/]*/[^/]*))").env_type as $key | {($key) : [.]}) as $item ({}; . *= $item)'
And obtained:
{
"nonprod/global": [
"nonprod/global/app/dev03"
],
"prod/na": [
"prod/na/app/prod01"
],
"test/eu": [
"test/eu/app/cust01"
],
"prod/eu": [
"prod/eu/app/prod01"
]
}
I'm expecting:
{
"nonprod/global":[
"nonprod/global/app/sit01",
"nonprod/global/app/dev02",
"nonprod/global/app/dev03"
],
"prod/eu":[
"prod/eu/app/prod01"
],
"prod/na":[
"prod/na/app/prod01"
],
"test/eu":[
"test/eu/app/cust01"
]
}
答案1
得分: 2
"which would have as key the first two words" 可以翻译为:"将前两个单词作为键"
"You can def
ine a function that splits at the second slash, then usi it to group_by
the input array, creating an array of arrays, then use INDEX
to turn it into an object, using the same function on the first
item." 可以翻译为:"您可以定义一个在第二个斜杠处拆分的函数,然后将其用于对输入数组进行分组,创建一个数组的数组,然后使用 INDEX
将其转换为对象,在第一个项目上使用相同的函数。"
以下是代码部分,不需要翻译:
def f: .[:indices("/")[1]];
group_by(f) | INDEX(first | f)
{
"nonprod/global": [
"nonprod/global/app/sit01",
"nonprod/global/app/dev02",
"nonprod/global/app/dev03"
],
"prod/na": [
"prod/na/app/prod01"
],
"test/eu": [
"test/eu/app/cust01"
]
}
英文:
> which would have as key the first two words
You can def
ine a function that splits at the second slash, then usi it to group_by
the input array, creating an array of arrays, then use INDEX
to turn it into an object, using the same function on the first
item.
def f: .[:indices("/")[1]];
group_by(f) | INDEX(first | f)
{
"nonprod/global": [
"nonprod/global/app/sit01",
"nonprod/global/app/dev02",
"nonprod/global/app/dev03"
],
"prod/na": [
"prod/na/app/prod01"
],
"test/eu": [
"test/eu/app/cust01"
]
}
答案2
得分: 2
使用 +=
向数组添加元素。如果键不存在,jq将即时创建它(null + []
的结果是[]
):
reduce (.[] | capture("^(?<env>(?<type>[^/]*/[^/]*).*)$")) as {$env, $type}
({}; .[$type] += [$env])
{
"nonprod/global": [
"nonprod/global/app/sit01",
"nonprod/global/app/dev02",
"nonprod/global/app/dev03"
],
"prod/na": [
"prod/na/app/prod01"
],
"test/eu": [
"test/eu/app/cust01"
]
}
英文:
Use +=
to add to an array. If the key doesn't exist, jq will create it on the fly (null + []
evaluates to []
):
reduce (.[] | capture("^(?<env>(?<type>[^/]*/[^/]*).*)$")) as {$env, $type}
({}; .[$type] += [$env])
{
"nonprod/global": [
"nonprod/global/app/sit01",
"nonprod/global/app/dev02",
"nonprod/global/app/dev03"
],
"prod/na": [
"prod/na/app/prod01"
],
"test/eu": [
"test/eu/app/cust01"
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论