这是一个假设您的数据是干净的并且您需要的替换是有限的解决方案。
.location = (
{
"Boston, MA": "Boston, Massachussettts",
"San Francisco, CA": "San Francisco, California",
"New York City, NY": "New York City, New York"
}[.location] // .location
)
iffilter.jq
包含上述过滤器并input.json
包含
{ "name": "Bob", "location": "Boston, MA" }
{ "name": "Peter", "location": "San Francisco, CA" }
{ "name": "Jane", "location": "New York City, NY" }
{ "name": "Unknown", "location": "Unknown" }
然后
jq -M -c -f filter.jq input.json
会产生
{"name":"Bob","location":"Boston, Massachussettts"}
{"name":"Peter","location":"San Francisco, California"}
{"name":"Jane","location":"New York City, New York"}
{"name":"Unknown","location":"Unknown"}
如果查找失败,请注意使用//
保持位置不变。