注意你需要一个jsonnet
从 master 构建的二进制文件,截至std.parseJson()
2019 年 2 月 26 日尚未发布。
输入.json
{
"apiVersion": "v1",
"items": [
{
"apiVersion": "v1",
"data": {
"alok-pod.json": "{\n \"namespace\": \"alok\",\n \"editable\": true\n}"
}
},
]
}
edit_ns.jsonnet
// edit_ns.jsonnet for https://stackoverflow.com/questions/54880959/make-changes-to-json-string-using-jsonnet
//
// NOTE: as of 2019-02-26 std.parseJson() is unreleased,
// need to build jsonnet from master.
local input = import "input.json";
local edit_ns_json(json, ns) = (
std.manifestJson(std.parseJson(json) { namespace: ns })
);
local edit_ns(x, ns) = (
x {
local d = super.data,
data+: {
[key]: edit_ns_json(d[key], ns) for key in std.objectFields(d)
}
}
);
[edit_ns(x, "foo") for x in input.items]
示例运行:
$ jsonnet-dev edit_ns.jsonnet
[
{
"apiVersion": "v1",
"data": {
"alok-pod.json": "{\n \"editable\": true,\n \"namespace\": \"foo\"\n}"
}
}
]