0

I am having trouble with creating and then using a variable that contains double quotes in the value and have really struggled to find any other posts with such an example, some with special characters but none specifically looking at using double quotes.

I am looking to create a variable in a bash script that will allow me to perform the following CURL command with the "text" value as the variable

curl -v -o POST \
-u "apikey:-------------------------------------" \
-H "Content-Type: application/json" \
-d '{
  "text":"Hello World",
  "features": {
  "sentiment": {},
  "categories": {},
  "concepts": {},
  "keywords": {},
  "emotion": {} 
  }
}' \
"https://gateway-wdc.watsonplatform.net/natural-language- 
understanding/api/v1/analyze?version=2018-03-19" 
$SHELL

I have tried the below but, although the echo looks like it was passing the right value, the CURL response is a 400 error - invalid response.

VAR1='"Hello World"'

echo "VAR1=${VAR1}"
echo 

curl -v -o POST \
-u "apikey:-------------------------------------" \
-H "Content-Type: application/json" \
-d '{
     "text":${VAR1},
     "features": {
     "sentiment": {},
     "categories": {},
     "concepts": {},
     "keywords": {},
     "emotion": {}  
    }
}' \
"https://gateway-wdc.watsonplatform.net/natural-language- 
understanding/api/v1/analyze?version=2018-03-19"
$SHELL
2

1 Answer 1

2

(There is a duplicate for this, but I can never find it.)

Use a tool like jq to build the JSON.

data=$(jq --argjson x "$VAR1" '
  {
    text: $x,
    features: {},
    sentiment: {},
    categories: {},
    concepts: {},
    keywords: {},
    emotion: {}  
  }'
)
curl ... -d "$data" ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.