我想使用jsonnet在下面的json中更改namespace
中的alok-pod.json
。
{
"apiVersion": "v1",
"items": [
{
"apiVersion": "v1",
"data": {
"alok-pod.json": "{\n \"namespace\": \"alok\",\n \"editable\": true,\n}"
}
},
]
}
请提出如何使用jsonnet做到这一点?
最佳答案
注意您需要从master生成的jsonnet
二进制文件,因为std.parseJson()
截至2019-02-26尚未发布。
{
"apiVersion": "v1",
"items": [
{
"apiVersion": "v1",
"data": {
"alok-pod.json": "{\n \"namespace\": \"alok\",\n \"editable\": true\n}"
}
},
]
}
// 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}"
}
}
]
关于json - 使用jsonnet更改json字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54880959/