Home > front end >  Json string parsing with shell script
Json string parsing with shell script

Time:12-05

I have issue in inoking curl ,but I am not understanding where I misssed curly brace

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 Tech AIML DevOps/_apis/build/builds?api-version=5.0' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
--data $json

I am getting this error always

                                 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"}}'

CodePudding user response:

I find using to generate JSON can be quite readable:

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
            }
        }'
)

results in $json having the value

{"definition":{"id":5474},"sourceBranch":"refs/heads/feature/node-memory-clean-up","parameters":{"system.debug":"false","Cluster_name":"testclu","Node_name":"testnode"}}

then

curl ... --data "$json" ...
  • Related