Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell variable expansion in single quoted string


I need to build a JSON variable that contains another JSON formatted content which should be formatted as a string. I'm putting the inner JSON into single quote so it wont get parsed along with the outer JSON. The inner JSON contains a variable that should be extended. Unfortunately, the single quotes prevent this.
Example:

$link = "http://www.google.de"
$event = @{
   var1 = "sys"
   var2 = "yadda"
   inner_json = '"System": {"link":"$link"}}'
}
$json = $event | ConvertTo-Json

The variable $inner_json must be embedded as a string. Is there any way to enforce the expansion of $link within the single quotes? Any other idea how to solve this problem? Any help is apprectiated.
edit: I would be expecting the inner_json like this:

{"var1":"sys", "var2":"yadda", "inner_json": "{\"System\": {\"link\":\"google.de\"}}"}
like image 619
user3322838 Avatar asked May 01 '26 16:05

user3322838


1 Answers

If you prepare the object (using a HashTable or PSCustomObject), variables (starting with a $, including $var1, $inner_json) will be automatically expanded anyway.
I think you want to do this:

$link = "http://www.google.de"
$event = @{
    var1 = 'sys'
    var2 = 'yadda'
    inner_json = @{
        System = @{
            link = $link
        }
    }
}
$event | ConvertTo-Json

{
  "inner_json": {
    "System": {
      "link": "http://www.google.de"
    }
  },
  "var2": "yadda",
  "var1": "sys"
}

Based on the added expectation:
*Note:
Double quotes are automatically escaped with a backslash (\) in json.
To escape a double quote in PowerShell, use the backtick: (`)

$link = "http://www.google.de"
$event = @{
    var1 = 'sys'
    var2 = 'yadda'
    inner_json = "{`"System`": {`"link`":`"$Link`"}}"
}
$event | ConvertTo-Json

{
  "inner_json": "{\"System\": {\"link\":\"http://www.google.de\"}}",
  "var2": "yadda",
  "var1": "sys"
}

But it is better to stay away from fabricating a Json string (including the inner Json string) and also create the inner Json from an object and literally embed that string into the outer Json:

$link = "http://www.google.de"
$event = @{
    var1 = 'sys'
    var2 = 'yadda'
    inner_json = @{System = @{link = $Link}} | ConvertTo-Json -Compress
}
$event | ConvertTo-Json

{
  "inner_json": "{\"System\":{\"link\":\"http://www.google.de\"}}",
  "var2": "yadda",
  "var1": "sys"
}
like image 94
iRon Avatar answered May 04 '26 07:05

iRon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!