[英]Json string parsing with shell script
我在调用 curl 时遇到问题,但我不明白我在哪里错过了花括号
CLUSTER_NAME=testclu
Node_name=testnode
json=\''{"definition":{"id":5474},"sourceBranch": "refs/heads/feature/node-memory-clean-up","parameters": {"system.debug":"false","Cluster_name":"'"$CLUSTER_NAME"'","Node_name":"'"$Node_name"'"}}'\'
printf '%s\n' "$json"
curl -X POST \
-u 'xxyv.x.xxx https://dev.azure.com/sample/RD%20Tech%20AIML%20DevOps/_apis/build/builds?api-version=5.0' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
--data $json
我总是收到这个错误
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: "refs
curl: (3) unmatched close brace/bracket in URL position 72:
{"system.debug":"false","Cluster_name":"testclu","Node_name":"testnode"}}'
我发现使用jq生成JSON 的可读性很强:
json=$(
jq -n -c \
--arg cname "$CLUSTER_NAME" \
--arg nname "$Node_name" \
'{
definition: {id: 5474},
sourceBranch: "refs/heads/feature/node-memory-clean-up",
parameters: {
"system.debug": "false",
Cluster_name: $cname,
Node_name: $nname
}
}'
)
导致$json
具有值
{"definition":{"id":5474},"sourceBranch":"refs/heads/feature/node-memory-clean-up","parameters":{"system.debug":"false","Cluster_name":"testclu","Node_name":"testnode"}}
然后
curl ... --data "$json" ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.