Search code examples
jsonpowershellsingle-quotesvariable-expansion

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

Solution

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