【问题标题】:Replace value in json during run time在运行时替换 json 中的值
【发布时间】:2019-03-08 19:29:51
【问题描述】:

我正在尝试替换 json 中运行时的值,即

{
  "containerDefinitions": [{
    "name": "containername",
    "image": "myimage",
    "memory": 512,
    "cpu": 1,
    "essential": true,
    "portMappings": [{
      "hostPort": 80,
      "containerPort": 80,
      "protocol": "tcp"
    }]
  }],
  "volumes": [],
  "family": "containername"
}

应该是新的

{
  "containerDefinitions": [{
    "name": "containername",
    "image": "new image",
    "memory": 512,
    "cpu": 1,
    "essential": true,
    "portMappings": [{
      "hostPort": 80,
      "containerPort": 80,
      "protocol": "tcp"
    }]
  }],
  "volumes": [],
  "family": "containername"
}
  • 旧值:-“image”:“myimage”
  • 新值:-“图像”:“新图像”

我想用 bash 做。有什么最好的办法吗?可以通过jq吗?

【问题讨论】:

  • 是特殊的还是普通的换词?
  • 值可以是任何东西。所以它应该检查关键字“image”并替换值

标签: bash


【解决方案1】:

您可以为此使用 jq:

jq '.containerDefinitions[].image="new image"' old.son

【讨论】:

    【解决方案2】:

    您应该真正使用理解 JSON 的工具或库。但如果您的输入如上所示,您可以使用sed:

    $ sed -i '/"image": "myimage"/s/"myimage"/"new image"/' input.json
    

    【讨论】:

    • 除了简单的事情之外,这对任何事情都不起作用。提供的任何解决方案都应确保正确的 json 编码。
    【解决方案3】:

    鉴于linux 本身不允许重定向到同一个文件,您可以使用moreutils 包中的sponge 实用程序

    - apt update && apt install -y moreutils
    - jq '.json_label="json_value"' file.json | sponge file.json
    

    这将真正允许在线替换;

    【讨论】:

      【解决方案4】:

      这基本上是用户建议的,在极简脚本中! 这不使用外部程序,但依赖于 bash!

      您的新 json 将存储在 newStr 变量中。

      #!/bin/bash
      
      str='
      {
        "containerDefinitions": [{
          "name": "containername",
          "image": "myimage",
          "memory": 512,
          "cpu": 1,
          "essential": true,
          "portMappings": [{
            "hostPort": 80,
            "containerPort": 80,
            "protocol": "tcp"
          }]
        }],
        "volumes": [],
        "family": "containername"
      }'
      
      replaceValue='"myimage"'
      replaceWith='"new image"'
      newStr=${str//$replaceValue/$replaceWith}
      echo $newStr
      

      输出:

      { "containerDefinitions": [{ "name": "containername", "image": "new 图像”,“内存”:512,“CPU”:1,“基本”:真,“端口映射”:[{ “主机端口”:80,“容器端口”:80,“协议”:“tcp”}]}], "volumes": [], "family": "containername" }

      【讨论】:

      • 很好,谢谢。现在我明白了,一切对我来说都很好。
      • 和我一起工作。应该被选为接受的答案。
      【解决方案5】:

      这样的东西应该适合你:

      oldValue='"image": "myimage"'
      newValue=${oldValue//myimage/newImage}
      echo newValue
      

      oldValue 中应该是你的 json。

      【讨论】:

      • 我想改变 json 而不仅仅是值
      • 所以我在oldValue 中写道应该是你的json。
      【解决方案6】:

      如果python可靠存在,那么您可以像这样使用嵌入式python:

      • 设置多行字符串
      • 执行并在错误时失败

        #!/usr/bin/evn bash
        # python as multi line string
        
        read -r -d '' PYSCRIPT << ENDPY
        import json
        import os
        jsonfile = 'daemon.json'
        data = {}
        if os.path.exists(jsonfile):
            data = json.load(open(jsonfile))
        data["booleanVar"] = True
        data["stringVar"] = "foo"
        with open(jsonfile, 'w') as fp:
            json.dump(data, fp)
        ENDPY
        
        # execute python and fail on error
        
        echo "$PYSCRIPT" |python - || exit 1
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-25
        • 1970-01-01
        • 1970-01-01
        • 2014-05-13
        相关资源
        最近更新 更多