Link to home
Start Free TrialLog in
Avatar of Joost Kuin
Joost KuinFlag for Netherlands

asked on

Cannot resolve variable within JSON file

In the example code below, I'm attempting to set variable varB to the same value as varA.

$strJSONdata = @"
    {
        "variables": {
            "varA": "myVariable",
            "varB": "[variables('varA')]"
        }
    }
"@

$objJSON = $strJSONdata | ConvertFrom-Json
write-host $objJSON.variables.varA
write-host $objJSON.variables.varB

Open in new window


But when i execute this script, the ouput is:
myVariable
[variables('varA')]

Open in new window


To be clear, I expected the output to be:
myVariable
myVariable

Open in new window


What is the correct syntax to refer to the value of another variable within the same JSON file.

By the way, I based my example on this web page:
https://docs.microsoft.com/en-us/azure/architecture/building-blocks/extending-templates/objects-as-parameters
Avatar of footech
footech
Flag of United States of America image

Just because Azure may interpret JSON files formatted a specific way for their templates, doesn't mean it has any relation to PowerShell.  PowerShell can read JSON, but essentially all you get is properties (and subproperties) and their values.  It doesn't try to interpret the values as anything other than strings (at least to my knowledge).
Avatar of Joost Kuin

ASKER

@Footech: Thank you for your reply. So basically there is no native support in JSON for values to refer to another value within a single file?
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
although not the answer I was hoping for. At least I can end my search (which was driving me nuts).

@footech: Thanks for your help!