有没有办法在 jq 的对象构造中使用默认值?
Is there a way to use default in object construction with jq?
我想根据条件从数组中过滤和分配一个值,并在数组没有匹配对象的情况下使用默认值。
这是一个示例对象:
{
"array" : [
{
"id": "A",
"conversations": [
{
"conversation": "1",
"type": "good"
},
{
"conversation": "2",
"type": "bad"
}
]
},
{
"id": "B",
"conversations": [
{
"conversation": "3",
"type": "good"
},
{
"conversation": "4",
"type": "bad"
}
]
},
{
"id": "C",
"conversations": [
{
"conversation": "5",
"type": "bad"
},
{
"conversation": "6",
"type": "bad"
}
]
}
]
}
所需输出:
{
"id": "A",
"goodConversation": "1"
}
{
"id": "B",
"goodConversation": "3"
},
{
"id": "C",
"goodConversation": null
}
我输入的回显:
echo '{"array":[{"id":"A","conversations":[{"conversation":"1","type":"good"},{"conversation":"2","type":"bad"}]},{"id":"B","conversations":[{"conversation":"3","type":"good"},{"conversation":"4","type":"bad"}]},{"id":"C","conversations":[{"conversation":"5","type":"bad"},{"conversation":"6","type":"bad"}]}]}'
我尝试 运行 关注 jq
jq '.array[] | {id, "goodConversation": .conversations[] | select(.type == "good") | .conversation}'
实际输出:
{
"id": "A",
"goodConversation": "1"
}
{
"id": "B",
"goodConversation": "3"
}
由于 ID 为 "C" 的对象没有任何良好的对话,因此整个对象都被过滤掉了。有没有办法创建包含 "C" 和 null
作为值的输出对象?
澄清:
- "conversations" 最多会有一次愉快的对话。
- 我正在使用 jq 1.5
提供默认值的一种方法通常是使用 //
"alternative" 运算符。在你奠定的基础上,你可以写:
.array[]
| {id,
"goodConversation":
((.conversations[]
| select(.type == "good")
| .conversation) // null) }
但是,如果有多于一个 "good" 对话,这可能不是您想要的。如果不是,则考虑使用 first
,例如:
.array[]
| {id,
"goodConversation":
( first(.conversations[]
| select(.type == "good")
| .conversation) // null)}
我想根据条件从数组中过滤和分配一个值,并在数组没有匹配对象的情况下使用默认值。
这是一个示例对象:
{
"array" : [
{
"id": "A",
"conversations": [
{
"conversation": "1",
"type": "good"
},
{
"conversation": "2",
"type": "bad"
}
]
},
{
"id": "B",
"conversations": [
{
"conversation": "3",
"type": "good"
},
{
"conversation": "4",
"type": "bad"
}
]
},
{
"id": "C",
"conversations": [
{
"conversation": "5",
"type": "bad"
},
{
"conversation": "6",
"type": "bad"
}
]
}
]
}
所需输出:
{
"id": "A",
"goodConversation": "1"
}
{
"id": "B",
"goodConversation": "3"
},
{
"id": "C",
"goodConversation": null
}
我输入的回显:
echo '{"array":[{"id":"A","conversations":[{"conversation":"1","type":"good"},{"conversation":"2","type":"bad"}]},{"id":"B","conversations":[{"conversation":"3","type":"good"},{"conversation":"4","type":"bad"}]},{"id":"C","conversations":[{"conversation":"5","type":"bad"},{"conversation":"6","type":"bad"}]}]}'
我尝试 运行 关注 jq
jq '.array[] | {id, "goodConversation": .conversations[] | select(.type == "good") | .conversation}'
实际输出:
{
"id": "A",
"goodConversation": "1"
}
{
"id": "B",
"goodConversation": "3"
}
由于 ID 为 "C" 的对象没有任何良好的对话,因此整个对象都被过滤掉了。有没有办法创建包含 "C" 和 null
作为值的输出对象?
澄清:
- "conversations" 最多会有一次愉快的对话。
- 我正在使用 jq 1.5
提供默认值的一种方法通常是使用 //
"alternative" 运算符。在你奠定的基础上,你可以写:
.array[]
| {id,
"goodConversation":
((.conversations[]
| select(.type == "good")
| .conversation) // null) }
但是,如果有多于一个 "good" 对话,这可能不是您想要的。如果不是,则考虑使用 first
,例如:
.array[]
| {id,
"goodConversation":
( first(.conversations[]
| select(.type == "good")
| .conversation) // null)}