将多行JSON插入YAML

发布于 2025-02-11 08:17:17 字数 977 浏览 1 评论 0原文

拥有一个看起来像这样的 configmap.yaml 文件:

apiVersion: v1
kind: ConfigMap
metadata:
  name: someCF
data:
  appsettings.k8s.json: |
   {
    "Logging": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information"
       }
     }
   }

还有另一个名为json1的json文件,我需要将其放入data.appsettings.k8s.json:

    {
    "Logging": {
      "LogLevel": {
        "Default": "Full",
        "Microsoft": "Error",
        "Microsoft.Hosting.Lifetime": "Information"
       }
     }
   }

如何更改 data.appsettings中的json .k8s.json ,看起来会这样:

apiVersion: v1
kind: ConfigMap
metadata:
  name: someCF
data:
  appsettings.k8s.json: |
    {
      "Logging": {
        "LogLevel": {
          "Default": "Full",
          "Microsoft": "Error",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      }
    }

Have a ConfigMap.yaml file that looks like this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: someCF
data:
  appsettings.k8s.json: |
   {
    "Logging": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information"
       }
     }
   }

Also have another JSON file named JSON1 that I need to put into data.appsettings.k8s.json:

    {
    "Logging": {
      "LogLevel": {
        "Default": "Full",
        "Microsoft": "Error",
        "Microsoft.Hosting.Lifetime": "Information"
       }
     }
   }

How to change JSON in data.appsettings.k8s.json so it will look like this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: someCF
data:
  appsettings.k8s.json: |
    {
      "Logging": {
        "LogLevel": {
          "Default": "Full",
          "Microsoft": "Error",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

献世佛 2025-02-18 08:17:17

请注意,您还可以将文件作为字符串直接加载:

yq '.data."appsettings.k8s.json" = loadstr("json_file")' yaml

免责声明:我写了YQ

Note that you can also load the file as a string direct like this:

yq '.data."appsettings.k8s.json" = loadstr("json_file")' yaml

disclaimer: I wrote yq

玩心态 2025-02-18 08:17:17

您可以精心打印JSON文件,并将其作为参数传递给YQ。由于 mikefarah/yq 允许您更新YAML多行,您可以做。

style 对于appsettings.k8s.k8s.jsons.json文字上,因此不必修改它,

JSON="$(yq -o=json json_file)" yq '.data."appsettings.k8s.json" = strenv(JSON)' yaml

请使用-i- Inplace flag即可更新文件Inploph。在版本4.25.3上测试

You can pretty-print the JSON file and pass that as an argument to yq. Since mikefarah/yq allows you to update YAML multi-line, you could just do.

The style for the appsettings.k8s.json is already at literal, so it doesn't have to be modified

JSON="$(yq -o=json json_file)" yq '.data."appsettings.k8s.json" = strenv(JSON)' yaml

Use the -i or --inplace flag to update the file inplace. Tested on version 4.25.3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文