0

Using Powershell, I'm trying to PUT data into an API, but I'm having trouble using a variable within the JSON:

This code below does not generate an error from the API, but it PUTS the singer as, literally, "$var_currentsinger" and doesn't use the intended variable

$currentsinger = "Michael Jackson"
$headers.Add("Content-Type", "application/json")
$response = Invoke-WebRequest -Uri https://stackoverflow.com -Method PUT -Headers $headers -Body '{
  "album": {
    "name": "Moonlight Sonata",
    "custom_fields": [{
      "singer": "$currentsinger",
      "songwriter": "Etta James"
    }]
  }
}'

This version below does not work, I assume because there are no quotes around the singer name. The API returns a value that the data is invalid

$currentsinger = "Michael Jackson"
$headers.Add("Content-Type", "application/json")
$response = Invoke-WebRequest -Uri https://stackoverflow.com -Method PUT -Headers $headers -Body '{
  "album": {
    "name": "Moonlight Sonata",
    "custom_fields": [{
      "singer": $currentsinger,
      "songwriter": "Etta James"
    }]
  }
}'

The only thing I have tried is double quotes and triple quotes around the variable, but I either get the $currentsinger variable within the JSON and have it submit the variable value and not the variable name.

5
  • 1
    json often (always?) only allows double-quotes. You can escape each internal double-quote with backtick, or join single-quoted strings together Commented May 25, 2022 at 19:33
  • 1
    try with double quoted here-string -Body @" .... "@ Commented May 25, 2022 at 19:38
  • @Cpt.Whale Can you make that an answer? I don't know if I understand how to implement what you're saying to try. Commented May 25, 2022 at 20:35
  • @Cpt.Whale I got it done based on your advice. Please make it an answer and I'll accept it. I ended up doing backtickdoublequote for each internal double quote (too hard to put as inline comment code, so I'll paste my final code once you post as an answer) Commented May 25, 2022 at 20:48
  • Instead of generating the json text yourself, consider creating a nested hashtable and using ConvertTo-Json as it will handle a whole multitude of edge-case for you - for example, what if $currentsinger = “James `”Godfather of Soul`” Brown” - you’d end up with invalid json by just interpolating the variable value into a json string Commented May 26, 2022 at 8:15

1 Answer 1

2

JSON requires double-quotes, so one example way to handle is by escaping the quotes or with a double-quoted here-string:

# here-string
$json = @"
  "singer": $currentsinger,
  "songwriter": "Etta James"
"@

# escaped
$json = "
  `"singer`": $currentsinger,
  `"songwriter`": `"Etta James`"
"
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.