将 Helm 中的映射类型数据转换为 JSON 对象。

huangapple go评论103阅读模式
英文:

helm convert map type data into json object

问题

我可以帮你翻译这段内容。以下是翻译的结果:

我在我的values.yaml文件中有地图数据。

values.yaml文件内容如下:

shapes:
  "FIRST SHAPE": "square"
  "SECOND SHAPE": "circle"
  "THIRD SHAPE": "triangle"

我需要将这些数据转换为JSON格式,并添加额外的JSON键值对属性"FOURTH_SHAPE":"rectangle",该属性在values.yaml文件中不存在。

我希望最终的JSON格式如下:

{"FIRST SHAPE": "square","SECOND SHAPE": "circle","THIRD SHAPE": "triangle","FOURTH_SHAPE":"rectangle"}

请问如何实现这个目标?我尝试了toJson函数,但它没有起作用。非常感谢您的帮助。

英文:

i have map data in my values.yaml

values.yaml

shapes:
  "FIRST SHAPE": "square"
  "SECOND SHAPE": "circle"
  "THIRD SHAPE": "triangle"

i need to convert this as json in my config-map. Also i need to add additional json key value attribute "FOURTH_SHAPE":"rectangle" which will not be available in values.yaml.

I want end json to be

{"FIRST SHAPE": "square","SECOND SHAPE": "circle","THIRD SHAPE": "triangle","FOURTH_SHAPE":"rectangle"}

how do i get this done? i tried toJson function and it doesn't work. Any help appreciated

答案1

得分: 2

使用toJson函数和merge变体之一。

{{ merge .Values.shapes (dict "FOURTH_SHAPE" "rectangle") | toJson }}

如果你只想添加一个单独的值,你也可以使用set

{{ set .Values.shapes "FOURTH_SHAPE" "rectangle" | toJson }}
英文:

Use the toJson function annd one of the merge variants.

{{ merge .Values.shapes (dict "FOURTH_SHAPE" "rectangle") | toJson }}

If you only want to add a single value, you can also use set.

{{ set .Values.shapes "FOURTH_SHAPE" "rectangle" | toJson }}

huangapple
  • 本文由 发表于 2022年5月20日 15:16:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/72314961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定