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\"}}"}
$event | ConvertTo-Json? You can possibly solve this by using nested hashtables, but seeing your desired json would make sure...$inner_json = """System"": {""link"":""$($link)""}}"