bash 在运行时替换 json 中的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35868649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace value in json during run time
提问by Sandeep Sharma
I am trying to replace value on run time in json i.e.
我正在尝试在 json 中替换运行时的值
Old
老的
{
"containerDefinitions": [{
"name": "containername",
"image": "myimage",
"memory": 512,
"cpu": 1,
"essential": true,
"portMappings": [{
"hostPort": 80,
"containerPort": 80,
"protocol": "tcp"
}]
}],
"volumes": [],
"family": "containername"
}
New should be
新的应该是
{
"containerDefinitions": [{
"name": "containername",
"image": "new image",
"memory": 512,
"cpu": 1,
"essential": true,
"portMappings": [{
"hostPort": 80,
"containerPort": 80,
"protocol": "tcp"
}]
}],
"volumes": [],
"family": "containername"
}
- Old value: - "image": "myimage"
- New Value: - "image": "new image"
- 旧值:-“图像”:“我的图像”
- 新值:-“图像”:“新图像”
I want to do in bash. Is there any best way to do? Can we do through jq?
我想在 bash 中做。有什么最好的办法吗?可以通过jq吗?
回答by 7171u
You can use jq for this:
您可以为此使用 jq:
jq '.containerDefinitions[].image="new image"' old.son
回答by Sandeep Sharma
回答by cb0
This is basically what user suggested, inside a minimalist script! This uses no external programs but depends on bash!
这基本上是用户建议的,在极简脚本中!这不使用外部程序,而是依赖于 bash!
Your new json will be stored in the newStr
variable.
您的新 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
Output:
输出:
{ "containerDefinitions": [{ "name": "containername", "image": "new image", "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" }
回答by Mat
Something like this should works for you:
像这样的事情应该适合你:
oldValue='"image": "myimage"'
newValue=${oldValue//myimage/newImage}
echo newValue
In oldValue
should be your json.
在oldValue
应该是你的json。
回答by Peter Kahn
If python is reliably present, then you can use embedded python like so:
如果python可靠存在,那么您可以像这样使用嵌入式python:
- setup multiline string
execute and fail on error
#!/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
- 设置多行字符串
执行并出错失败
#!/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
回答by pkaramol
Given that linux
does not by itself allow redirection to the same file, you can use the sponge
utility from moreutils
package
鉴于它linux
本身不允许重定向到同一文件,您可以使用包中的sponge
实用moreutils
程序
- apt update && apt install -y moreutils
- jq '.json_label="json_value"' file.json | sponge file.json
This will truly allow in line replacement;
这将真正允许在线更换;