这是一个基于jtc
unix实用程序的简单解决方案:
bash $ jtc -w'<.*>D:' -eu echo '"{}"' \; file.json
{
"amount": "1000",
"country": "GB",
"customer": "user",
"id": "1",
"plate": "BMT-216-A",
"pndNumber": "20000",
"zoneNumber": "4"
}
bash $
如果您想将更改直接应用到json文件中,请使用-f
开关,如下所示:
bash $ jtc -f -w'<.*>D:' -eu echo '"{}"' \; file.json
提议的解决方案将与任意结构化的json一起正常工作,例如:
bash $ jtc -w'<.*>D:' -eu echo '"{}"' \; file.json
{
"amount": "1000",
"country": "GB",
"customer": "user",
"id": "1",
"plate": "BMT-216-A",
"pndNumber": "20000",
"sub": {
"subvalue": "123"
},
"zoneNumber": "4"
}
bash $
- 如果您想引用空值,只需抛出一个walk-path
-w'<>n:'
- 如果您想引用布尔值,则抛出一个walk-path
-w'<any>b:'
同样,以相似的方式可以轻松完成反向任务(file.json
不对所有数字加引号):例如,已被“加引号”,以对所有数字不加引号:
bash $ jtc -w'<^\d+$>R:' -eu echo {-} \; file.json
{
"amount": 1000,
"country": "GB",
"customer": "user",
"id": 1,
"plate": "BMT-216-A",
"pndNumber": 20000,
"zoneNumber": 4
}
bash $
UPDATE:jtc
现在实现的最新版本是模板和名称空间。这样就不需要调用外部外壳程序了:
bash $ jtc -w'<.*>D:' -u'<.*>D:<val>v' -T'"{val}"' file.json
{
"amount": "1000",
"country": "GB",
"customer": "user",
"id": "1",
"plate": "BMT-216-A",
"pndNumber": "20000",
"zoneNumber": "4"
}
jtc
用户指南:https://github.com/ldn-softdev/jtc/blob/master/User%20Guide.md
{"a":{"b":1},"b":null}
以{ "a": "{\"b\":1}", "b": "null" }