jq:将逗号分隔值转换为 JSON
jq: convert comma seperated values into JSON
我有一个逗号分隔的 IP 地址字符串(一个或多个)
nodes=192.168.56.111,192.168.56.112,192.168.56.113
使用 jq
,我想创建一个如下所示的 JSON 文档:
{
"clusters": [
{
"nodes": [
{
"node": {
"hostnames": {
"manage": [
"192.168.56.111"
],
"storage": [
"192.168.56.111"
]
},
"zone": 1
},
"devices": [
{
"name": "/dev/sdb",
"destroydata": false
}
]
},
{
"node": {
"hostnames": {
"manage": [
"192.168.56.112"
],
"storage": [
"192.168.56.112"
]
},
"zone": 1
},
"devices": [
{
"name": "/dev/sdb",
"destroydata": false
}
]
},
{
"node": {
"hostnames": {
"manage": [
"192.168.56.113"
],
"storage": [
"192.168.56.113"
]
},
"zone": 1
},
"devices": [
{
"name": "/dev/sdb",
"destroydata": false
}
]
}
]
}
]
}
我目前拥有的:
nodes=192.168.56.111,192.168.56.112,192.168.56.113
jq --arg ip "$nodes" '.clusters[].nodes[].node.hostnames.manage |= ($ip | split(","))' <<<'{"clusters":[{"nodes":[{"node":{"hostnames":{"manage":[],"storage":[]},"zone":1},"devices":[{"name":"/dev/sdb","destroydata":false}]}]}]}'
这有两个问题:
.clusters[].nodes[].node
未复制。
.clusters[].nodes[].node.hostnames.storage
缺失。
您可以使用 map
的拆分结果在 .clusters[0].nodes
:
下创建数组
nodes=192.168.56.111,192.168.56.112,192.168.56.113
jq -R '
{clusters: [{nodes: (./"," | map({
node: {hostnames: {manage: [.], storage: [.]}, zone: 1},
devices: [{name: "/dev/sdb", destroydata: false}]
}))}]}
' <<< "$nodes"
{
"clusters": [
{
"nodes": [
{
"node": {
"hostnames": {
"manage": [
"192.168.56.111"
],
"storage": [
"192.168.56.111"
]
},
"zone": 1
},
"devices": [
{
"name": "/dev/sdb",
"destroydata": false
}
]
},
{
"node": {
"hostnames": {
"manage": [
"192.168.56.112"
],
"storage": [
"192.168.56.112"
]
},
"zone": 1
},
"devices": [
{
"name": "/dev/sdb",
"destroydata": false
}
]
},
{
"node": {
"hostnames": {
"manage": [
"192.168.56.113"
],
"storage": [
"192.168.56.113"
]
},
"zone": 1
},
"devices": [
{
"name": "/dev/sdb",
"destroydata": false
}
]
}
]
}
]
}
我有一个逗号分隔的 IP 地址字符串(一个或多个)
nodes=192.168.56.111,192.168.56.112,192.168.56.113
使用 jq
,我想创建一个如下所示的 JSON 文档:
{
"clusters": [
{
"nodes": [
{
"node": {
"hostnames": {
"manage": [
"192.168.56.111"
],
"storage": [
"192.168.56.111"
]
},
"zone": 1
},
"devices": [
{
"name": "/dev/sdb",
"destroydata": false
}
]
},
{
"node": {
"hostnames": {
"manage": [
"192.168.56.112"
],
"storage": [
"192.168.56.112"
]
},
"zone": 1
},
"devices": [
{
"name": "/dev/sdb",
"destroydata": false
}
]
},
{
"node": {
"hostnames": {
"manage": [
"192.168.56.113"
],
"storage": [
"192.168.56.113"
]
},
"zone": 1
},
"devices": [
{
"name": "/dev/sdb",
"destroydata": false
}
]
}
]
}
]
}
我目前拥有的:
nodes=192.168.56.111,192.168.56.112,192.168.56.113
jq --arg ip "$nodes" '.clusters[].nodes[].node.hostnames.manage |= ($ip | split(","))' <<<'{"clusters":[{"nodes":[{"node":{"hostnames":{"manage":[],"storage":[]},"zone":1},"devices":[{"name":"/dev/sdb","destroydata":false}]}]}]}'
这有两个问题:
.clusters[].nodes[].node
未复制。.clusters[].nodes[].node.hostnames.storage
缺失。
您可以使用 map
的拆分结果在 .clusters[0].nodes
:
nodes=192.168.56.111,192.168.56.112,192.168.56.113
jq -R '
{clusters: [{nodes: (./"," | map({
node: {hostnames: {manage: [.], storage: [.]}, zone: 1},
devices: [{name: "/dev/sdb", destroydata: false}]
}))}]}
' <<< "$nodes"
{
"clusters": [
{
"nodes": [
{
"node": {
"hostnames": {
"manage": [
"192.168.56.111"
],
"storage": [
"192.168.56.111"
]
},
"zone": 1
},
"devices": [
{
"name": "/dev/sdb",
"destroydata": false
}
]
},
{
"node": {
"hostnames": {
"manage": [
"192.168.56.112"
],
"storage": [
"192.168.56.112"
]
},
"zone": 1
},
"devices": [
{
"name": "/dev/sdb",
"destroydata": false
}
]
},
{
"node": {
"hostnames": {
"manage": [
"192.168.56.113"
],
"storage": [
"192.168.56.113"
]
},
"zone": 1
},
"devices": [
{
"name": "/dev/sdb",
"destroydata": false
}
]
}
]
}
]
}