小编San*_*rma的帖子

在运行时替换 json 中的值

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

老的

{
  "containerDefinitions": [{
    "name": "containername",
    "image": "myimage",
    "memory": 512,
    "cpu": 1,
    "essential": true,
    "portMappings": [{
      "hostPort": 80,
      "containerPort": 80,
      "protocol": "tcp"
    }]
  }],
  "volumes": [],
  "family": "containername"
}
Run Code Online (Sandbox Code Playgroud)

新的应该是

{
  "containerDefinitions": [{
    "name": "containername",
    "image": "new image",
    "memory": 512,
    "cpu": 1,
    "essential": true,
    "portMappings": [{
      "hostPort": 80,
      "containerPort": 80,
      "protocol": "tcp"
    }]
  }],
  "volumes": [],
  "family": "containername"
}
Run Code Online (Sandbox Code Playgroud)
  • 旧值:-“图像”:“我的图像”
  • 新值:-“图像”:“新图像”

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

bash

6
推荐指数
2
解决办法
1万
查看次数

将JSON数据解析为变量赋值格式

我试图将JSON数据解析为可变格式

[
  {
    "Name" : "a",
    "Value" : "1"
  },
  {
    "Name" : "b",
    "Value" : "2"
  },
  {
    "Name" : "c",
    "Value" : "3"
  }
]
Run Code Online (Sandbox Code Playgroud)

输出应该是这样的

a=1
b=2
c=3
Run Code Online (Sandbox Code Playgroud)

这是我尝试过的,但它没有给出预期的结果:

jq '.[].Value' file.txt 
Run Code Online (Sandbox Code Playgroud)

shell json jq

2
推荐指数
1
解决办法
147
查看次数

标签 统计

bash ×1

jq ×1

json ×1

shell ×1