将嵌套数组对象分组到 JQ 中的父键
Group nested array objects to parent key in JQ
我有 JSON 来自外部应用程序,格式如下:
{
"ticket_fields": [
{
"url": "https://example.com/1122334455.json",
"id": 1122334455,
"type": "tagger",
"custom_field_options": [
{
"id": 123456789,
"name": "I have a problem",
"raw_name": "I have a problem",
"value": "help_i_have_problem",
"default": false
},
{
"id": 456789123,
"name": "I have feedback",
"raw_name": "I have feedback",
"value": "help_i_have_feedback",
"default": false
},
]
}
{
"url": "https://example.com/6677889900.json",
"id": 6677889900,
"type": "tagger",
"custom_field_options": [
{
"id": 321654987,
"name": "United States,
"raw_name": "United States",
"value": "location_123_united_states",
"default": false
},
{
"id": 987456321,
"name": "Germany",
"raw_name": "Germany",
"value": "location_456_germany",
"default": false
}
]
}
]
}
最终目标是能够将数据放入 TSV,因为 custom_field_options 数组中的每个对象都按父 ID (ticket_fields.id
) 分组,然后转置这样每个对象都将在一行中表示,如下所示:
Ticket Field ID
Name
Value
1122334455
I have a problem
help_i_have_problem
1122334455
I have feedback
help_i_have_feedback
6677889900
United States
location_123_united_states
6677889900
Germany
location_456_germany
我已经能够将数据成功导出到 TSV,但它是按行读取的,并且没有保留顺序,如下所示:
使用jq -r '.ticket_fields[] | select(.type=="tagger") | [.id, .custom_field_options[].name, .custom_field_options[].value] | @tsv'
Ticket Field ID
Name
Name
Value
Value
1122334455
I have a problem
I have feedback
help_i_have_problem
help_i_have_feedback
6677889900
United States
Germany
location_123_united_states
location_456_germany
生产中的每个 custom_field_options
数组都可以包含任意数量的对象(每个对象不限于 2 个)。但我似乎对如何适当地将这些对象分组或映射到它们的父对象 ticket_fields.id
以及如何以干净的方式转置数据感到困惑。查询中提到了 select(.type=="tagger")
,因为 ticket_fields.type
有多个值需要过滤掉。
根据此处的另一个答案,我确实尝试了 jq -r '.ticket_fields[] | select(.type=="tagger") | map(.custom_field_options |= from_entries) | group_by(.custom_field_options.ticket_fields) | map(map( .custom_field_options |= to_entries))'
的变体,但没有成功。如有任何帮助,我们将不胜感激!
您需要两个嵌套迭代,每个数组中一个。将 .id
的值保存在变量中以便稍后访问它。
jq -r '
.ticket_fields[] | select(.type=="tagger") | .id as $id
| .custom_field_options[] | [$id, .name, .value]
| @tsv
'
我有 JSON 来自外部应用程序,格式如下:
{
"ticket_fields": [
{
"url": "https://example.com/1122334455.json",
"id": 1122334455,
"type": "tagger",
"custom_field_options": [
{
"id": 123456789,
"name": "I have a problem",
"raw_name": "I have a problem",
"value": "help_i_have_problem",
"default": false
},
{
"id": 456789123,
"name": "I have feedback",
"raw_name": "I have feedback",
"value": "help_i_have_feedback",
"default": false
},
]
}
{
"url": "https://example.com/6677889900.json",
"id": 6677889900,
"type": "tagger",
"custom_field_options": [
{
"id": 321654987,
"name": "United States,
"raw_name": "United States",
"value": "location_123_united_states",
"default": false
},
{
"id": 987456321,
"name": "Germany",
"raw_name": "Germany",
"value": "location_456_germany",
"default": false
}
]
}
]
}
最终目标是能够将数据放入 TSV,因为 custom_field_options 数组中的每个对象都按父 ID (ticket_fields.id
) 分组,然后转置这样每个对象都将在一行中表示,如下所示:
Ticket Field ID | Name | Value |
---|---|---|
1122334455 | I have a problem | help_i_have_problem |
1122334455 | I have feedback | help_i_have_feedback |
6677889900 | United States | location_123_united_states |
6677889900 | Germany | location_456_germany |
我已经能够将数据成功导出到 TSV,但它是按行读取的,并且没有保留顺序,如下所示:
使用jq -r '.ticket_fields[] | select(.type=="tagger") | [.id, .custom_field_options[].name, .custom_field_options[].value] | @tsv'
Ticket Field ID | Name | Name | Value | Value |
---|---|---|---|---|
1122334455 | I have a problem | I have feedback | help_i_have_problem | help_i_have_feedback |
6677889900 | United States | Germany | location_123_united_states | location_456_germany |
生产中的每个 custom_field_options
数组都可以包含任意数量的对象(每个对象不限于 2 个)。但我似乎对如何适当地将这些对象分组或映射到它们的父对象 ticket_fields.id
以及如何以干净的方式转置数据感到困惑。查询中提到了 select(.type=="tagger")
,因为 ticket_fields.type
有多个值需要过滤掉。
根据此处的另一个答案,我确实尝试了 jq -r '.ticket_fields[] | select(.type=="tagger") | map(.custom_field_options |= from_entries) | group_by(.custom_field_options.ticket_fields) | map(map( .custom_field_options |= to_entries))'
的变体,但没有成功。如有任何帮助,我们将不胜感激!
您需要两个嵌套迭代,每个数组中一个。将 .id
的值保存在变量中以便稍后访问它。
jq -r '
.ticket_fields[] | select(.type=="tagger") | .id as $id
| .custom_field_options[] | [$id, .name, .value]
| @tsv
'