【发布时间】:2021-06-22 14:37:32
【问题描述】:
我有一些如下所示的文档,我需要更改permanentLocation.id 并识别id 为空的所有流通笔记并将它们设置为uuid
{
"circulationNotes": [
{
"id": null,
"noteType": "Check in",
"note": "Original location pmgd1 Gov. Documents - 2nd Fl.",
"staffOnly": true,
"source": {
"id": "affd2a52-2f04-40e3-8da3-37c2de08e338",
"personal": {
"lastName": "Data",
"firstName": "Migration"
}
},
"date": null
},
{
"id": null,
"noteType": "Check out",
"note": "Original location pmgd1 Gov. Documents - 2nd Fl.",
"staffOnly": true,
"source": {
"id": "affd2a52-2f04-40e3-8da3-37c2de08e338",
"personal": {
"lastName": "Data",
"firstName": "Migration"
}
},
"date": null
}
],
"permanentLoanType": {
"id": "cf424b8f-ae2b-4d01-8365-b412c7815ba9",
"name": "PML 1st Fl. - Circulating"
}
}
我可以在文档中轻松设置permanentLocation.id
jq '(.permanentLocation.id = "1d0ec5e4-ba80-4eb9-8b0c-53da40d6335b")'
但是,当我尝试为循环注释设置一个 ID 时,它们为空
jq --arg CHECKIN $(uuidgen) '
(.permanentLocation.id = "1d0ec5e4-ba80-4eb9-8b0c-53da40d6335b")
| (.circulationNotes[] |(select(.noteType == "Check in" and .id == null) | .id = $CHECKIN ))'
{
"id": "4b1e8cef-c0e7-44b3-9228-553f99019aa4",
"noteType": "Check in",
"note": "Original location pmgd1 Gov. Documents - 2nd Fl.",
"staffOnly": true,
"source": {
"id": "affd2a52-2f04-40e3-8da3-37c2de08e338",
"personal": {
"lastName": "Data",
"firstName": "Migration"
}
},
"date": null
}
我只是得到匹配的第一个注释,而不是像使用 PermanentLocation.id 那样修改值。
当 .circulationNotes.id 为空(或任意数量的元素)时,我如何让它设置一个 uuid,同时返回完整的文档,以便我得到:
{
"circulationNotes": [
{
"id": bd5379f7-69fc-48f5-8461-8dcc59883ae0,
"noteType": "Check in",
"note": "Original location pmgd1 Gov. Documents - 2nd Fl.",
"staffOnly": true,
"source": {
"id": "affd2a52-2f04-40e3-8da3-37c2de08e338",
"personal": {
"lastName": "Data",
"firstName": "Migration"
}
},
"date": null
},
{
"id": 33f60901-33a8-43d1-a2ce-e22d8be87d15,
"noteType": "Check out",
"note": "Original location pmgd1 Gov. Documents - 2nd Fl.",
"staffOnly": true,
"source": {
"id": "affd2a52-2f04-40e3-8da3-37c2de08e338",
"personal": {
"lastName": "Data",
"firstName": "Migration"
}
},
"date": null
}
],
"permanentLoanType": {
"id": "cf424b8f-ae2b-4d01-8365-b412c7815ba9",
"name": "PML 1st Fl. - Circulating"
},
"permanentLocation": {
"id": "1d0ec5e4-ba80-4eb9-8b0c-53da40d6335b"
}
}
【问题讨论】:
-
你应该提供一个minimal reproducible example,那个JSON太长了。