user123456
user123456

Reputation: 374

How to include an argument in a JSON expression: "Problems parsing JSON"

I would like to have a script git-repository that create a github repository, taking one argument

# git-repository <my_repository_name>

I am however unable to include a $@ variable. The expression:

curl -u mygituser https://api.github.com/user/repos -d "{"name":"$@"}"

gives the error

{
  "message": "Problems parsing JSON",
  "documentation_url": "https://developer.github.com/v3/repos/#create"
}

How to include the $@ variable inside a JSON expression?

Upvotes: 0

Views: 57

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295363

Use jq to safely generate JSON in bash. In this case, since you want a string rather than an array, you should use $* rather than $@:

json=$(jq -nc --arg name "$*" '{"name": $name}')
curl -u mygituser https://api.github.com/user/repos -d "$json"

Upvotes: 1

Related Questions