我有一个包含的json文件 nulls as values for some keys, which I would like to replace with some specific value.
鉴于此输入:
{
"id": null,
"rows": [
{
"panels": [
{
"title": "Panel1",
"datasource": null
},
{
"title": "Panel2",
"datasource": null
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想拥有
{
"id": null,
"rows": [
{
"panels": [
{
"title": "Panel1",
"datasource": "mydb"
},
{
"title": "Panel2",
"datasource": "mydb"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
What I currently use is
sed 's/"datasource": null/"datasource": "mydb"/'
Run Code Online (Sandbox Code Playgroud)
This produces the output I need, but I keep thinking that it is a shame to use sed for this job, when there are tools like jq that can work on JSON in a much better way.
首先,您需要确定要更新的对象。由于您要将面板的空数据源设置为"mydb",因此可以执行以下操作:
$ jq '.rows[].panels[].datasource //= "mydb"' input.json
Run Code Online (Sandbox Code Playgroud)
如果要datasource在任何级别更新任何对象上的任何属性,可以使用..递归搜索它们。
$ jq '(.. | select(objects | has("datasource"))).datasource //= "mydb"' input.json
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2832 次 |
| 最近记录: |