当对象内部的值与指定值匹配时删除 JSON 对象 (jq)
Delete a JSON object when a value inside the object matches a designated value (jq)
目标是删除 JSON 文件中包含 JQ 脚本中指定的 key/value 对的整个对象。
例如,path
中具有 /unwanted-path/
的所有对象都将被删除:
input.json:
[
{
"path": "/path-1/",
"guide": "Guide 1"
},
{
"path": "/path-2/",
"guide": "Guide 2"
},
{
"path": "/unwanted-path/",
"guide": "Guide 3"
}
]
output.json:
[
{
"path": "/path-1/",
"guide": "Guide 1"
},
{
"path": "/path-2/",
"guide": "Guide 2"
}
]
就潜在的成功方法而言,这个 JQ 脚本是我最接近的,但当然它的意图不同,并没有实现目标。
jq '.[] | walk(if type == "object" then del(.path) else . end)' original.json > modified.json
有没有一种方法可以在那里添加逻辑来指定与 path
一起使用的键?
键将始终是 path
,但我会根据几个 path
值进行过滤。如果有一种方法可以一次过滤多个 path
值,那是理想的,但并不重要。
以下代码通过四个示例展示了如何解决您的任务
#!/bin/bash
FILE='
[
{
"path": "/path-1/",
"guide": "Guide 1"
},
{
"path": "/unwanted-path-2/",
"guide": "Guide 2"
},
{
"path": "/path-3/",
"guide": "Guide 3",
"sub": [
{
"path": "/unwanted-path-4/",
"guide": "Guide 4"
},
{
"path": "/path-5/",
"guide": "Guide 5"
},
[
{
"path": "/path-6/",
"guide": "Guide 6"
},
{
"path": "/unwanted-path-7/",
"guide": "Guide 7"
}
]
]
}
]
'
UNWANTED='/unwanted-path-2/'
echo "example 1: keep only objects in array that does not have the unwanted path '$UNWANTED' (narrow search)"
jq --arg unwantedPath "$UNWANTED" \
'map(select(.path != $unwantedPath))' <<< "$FILE"
UNWANTED='/unwanted-path-7/'
echo -e "\nexample 2: delete objects that have the unwanted path '$UNWANTED' exactly (deep search)"
jq --arg unwantedPath "$UNWANTED" \
'del(..| objects | select(.path == $unwantedPath)) ' <<< "$FILE"
UNWANTED='unwanted'
echo -e "\nexample 3: delete objects that have the unwanted path '$UNWANTED' partially (deep search)"
jq --arg unwantedPath "$UNWANTED" \
'del(..| objects | select(.path | index($unwantedPath) != null)) ' <<< "$FILE"
UNWANTED='["/unwanted-path-4/", "/unwanted-path-7/"]'
echo -e "\nexample 4: delete objects that have one of unwanted paths '$UNWANTED' (deep search)"
jq --argjson unwantedPath "$UNWANTED" \
'del(..| objects | select(.path | IN($unwantedPath[]))) ' <<< "$FILE"
输出示例 1:仅保留数组中没有不需要的路径“/unwanted-path-2/”的对象(窄搜索)
[
{
"path": "/path-1/",
"guide": "Guide 1"
},
{
"path": "/path-3/",
"guide": "Guide 3",
"sub": [
{
"path": "/unwanted-path-4/",
"guide": "Guide 4"
},
{
"path": "/path-5/",
"guide": "Guide 5"
},
[
{
"path": "/path-6/",
"guide": "Guide 6"
},
{
"path": "/unwanted-path-7/",
"guide": "Guide 7"
}
]
]
}
]
输出示例 2:完全删除具有不需要的路径“/unwanted-path-7/”的对象(深度搜索)
[
{
"path": "/path-1/",
"guide": "Guide 1"
},
{
"path": "/unwanted-path-2/",
"guide": "Guide 2"
},
{
"path": "/path-3/",
"guide": "Guide 3",
"sub": [
{
"path": "/unwanted-path-4/",
"guide": "Guide 4"
},
{
"path": "/path-5/",
"guide": "Guide 5"
},
[
{
"path": "/path-6/",
"guide": "Guide 6"
}
]
]
}
]
输出示例 3:部分删除具有不需要的路径 'unwanted' 的对象(深度搜索)
[
{
"path": "/path-1/",
"guide": "Guide 1"
},
{
"path": "/path-3/",
"guide": "Guide 3",
"sub": [
{
"path": "/path-5/",
"guide": "Guide 5"
},
[
{
"path": "/path-6/",
"guide": "Guide 6"
}
]
]
}
]
输出示例 4:删除具有不需要的路径之一的对象 '["/unwanted-path-4/", "/unwanted-path-7/"]'(深度搜索)
[
{
"path": "/path-1/",
"guide": "Guide 1"
},
{
"path": "/unwanted-path-2/",
"guide": "Guide 2"
},
{
"path": "/path-3/",
"guide": "Guide 3",
"sub": [
{
"path": "/path-5/",
"guide": "Guide 5"
},
[
{
"path": "/path-6/",
"guide": "Guide 6"
}
]
]
}
]
目标是删除 JSON 文件中包含 JQ 脚本中指定的 key/value 对的整个对象。
例如,path
中具有 /unwanted-path/
的所有对象都将被删除:
input.json:
[
{
"path": "/path-1/",
"guide": "Guide 1"
},
{
"path": "/path-2/",
"guide": "Guide 2"
},
{
"path": "/unwanted-path/",
"guide": "Guide 3"
}
]
output.json:
[
{
"path": "/path-1/",
"guide": "Guide 1"
},
{
"path": "/path-2/",
"guide": "Guide 2"
}
]
就潜在的成功方法而言,这个 JQ 脚本是我最接近的,但当然它的意图不同,并没有实现目标。
jq '.[] | walk(if type == "object" then del(.path) else . end)' original.json > modified.json
有没有一种方法可以在那里添加逻辑来指定与 path
一起使用的键?
键将始终是 path
,但我会根据几个 path
值进行过滤。如果有一种方法可以一次过滤多个 path
值,那是理想的,但并不重要。
以下代码通过四个示例展示了如何解决您的任务
#!/bin/bash
FILE='
[
{
"path": "/path-1/",
"guide": "Guide 1"
},
{
"path": "/unwanted-path-2/",
"guide": "Guide 2"
},
{
"path": "/path-3/",
"guide": "Guide 3",
"sub": [
{
"path": "/unwanted-path-4/",
"guide": "Guide 4"
},
{
"path": "/path-5/",
"guide": "Guide 5"
},
[
{
"path": "/path-6/",
"guide": "Guide 6"
},
{
"path": "/unwanted-path-7/",
"guide": "Guide 7"
}
]
]
}
]
'
UNWANTED='/unwanted-path-2/'
echo "example 1: keep only objects in array that does not have the unwanted path '$UNWANTED' (narrow search)"
jq --arg unwantedPath "$UNWANTED" \
'map(select(.path != $unwantedPath))' <<< "$FILE"
UNWANTED='/unwanted-path-7/'
echo -e "\nexample 2: delete objects that have the unwanted path '$UNWANTED' exactly (deep search)"
jq --arg unwantedPath "$UNWANTED" \
'del(..| objects | select(.path == $unwantedPath)) ' <<< "$FILE"
UNWANTED='unwanted'
echo -e "\nexample 3: delete objects that have the unwanted path '$UNWANTED' partially (deep search)"
jq --arg unwantedPath "$UNWANTED" \
'del(..| objects | select(.path | index($unwantedPath) != null)) ' <<< "$FILE"
UNWANTED='["/unwanted-path-4/", "/unwanted-path-7/"]'
echo -e "\nexample 4: delete objects that have one of unwanted paths '$UNWANTED' (deep search)"
jq --argjson unwantedPath "$UNWANTED" \
'del(..| objects | select(.path | IN($unwantedPath[]))) ' <<< "$FILE"
输出示例 1:仅保留数组中没有不需要的路径“/unwanted-path-2/”的对象(窄搜索)
[
{
"path": "/path-1/",
"guide": "Guide 1"
},
{
"path": "/path-3/",
"guide": "Guide 3",
"sub": [
{
"path": "/unwanted-path-4/",
"guide": "Guide 4"
},
{
"path": "/path-5/",
"guide": "Guide 5"
},
[
{
"path": "/path-6/",
"guide": "Guide 6"
},
{
"path": "/unwanted-path-7/",
"guide": "Guide 7"
}
]
]
}
]
输出示例 2:完全删除具有不需要的路径“/unwanted-path-7/”的对象(深度搜索)
[
{
"path": "/path-1/",
"guide": "Guide 1"
},
{
"path": "/unwanted-path-2/",
"guide": "Guide 2"
},
{
"path": "/path-3/",
"guide": "Guide 3",
"sub": [
{
"path": "/unwanted-path-4/",
"guide": "Guide 4"
},
{
"path": "/path-5/",
"guide": "Guide 5"
},
[
{
"path": "/path-6/",
"guide": "Guide 6"
}
]
]
}
]
输出示例 3:部分删除具有不需要的路径 'unwanted' 的对象(深度搜索)
[
{
"path": "/path-1/",
"guide": "Guide 1"
},
{
"path": "/path-3/",
"guide": "Guide 3",
"sub": [
{
"path": "/path-5/",
"guide": "Guide 5"
},
[
{
"path": "/path-6/",
"guide": "Guide 6"
}
]
]
}
]
输出示例 4:删除具有不需要的路径之一的对象 '["/unwanted-path-4/", "/unwanted-path-7/"]'(深度搜索)
[
{
"path": "/path-1/",
"guide": "Guide 1"
},
{
"path": "/unwanted-path-2/",
"guide": "Guide 2"
},
{
"path": "/path-3/",
"guide": "Guide 3",
"sub": [
{
"path": "/path-5/",
"guide": "Guide 5"
},
[
{
"path": "/path-6/",
"guide": "Guide 6"
}
]
]
}
]