我想要
"1": {
"id": "1",
"type": "select",
"label": "country :",
"choices": {
"1": {
"label": "Canada CAN",
"value": "",
},
"2": {
"label": "United States USA",
"value": ""
}
}
}
生产
"1": {
"id": "1",
"type": "select",
"label": "country :",
"choices": {
"1": {
"label": "Canada",
"value": "CAN",
},
"2": {
"label": "United States",
"value": "USA"
}
}
}
现在我有一个两步解决方案,由一个类似sed的函数组成
def do_extract:
if .value | test("^$") then
(.value = (.label | capture(".* (?<code>...)")).code) | .label = (.label | capture("(?<name>.*) ...$").name)
else
.
end;
并直接访问子树[]."1"."choices"|keys_unsorted as $key|map_values(do_extract)
,但是我必须手动复制粘贴输出来代替原始的“选择”字典。
是否可以执行此功能,但仍打印周围的数据?
谢谢
答案 0 :(得分:1)
要获得想要的结果,您可以这样做:
.[].choices[] |= (.label | capture("^(?<label>.*?) (?<value>[^ ]+)$"))
要在您的函数中执行此操作,请将其更改为:
def do_extract:
if .value == "" then
(.label | capture("^(?<label>.*?) (?<value>[^ ]+)$"))
else
.
end;
然后使用它:
.[].choices[] |= do_extract