Phil
Phil

Reputation: 139

How do I expand variables in a JSON string in Powershell?

Goal: Get information from one API request, store those into variables and pass into a new API request to make a JIRA issue.

Env:

Problem: I have no issues with the first two steps in the goal. But I'm not sure how to properly construct the last step.

$summary = 'summary'
$desc = 'desc'

$Body = '{
    "fields": {
       "project": {"key": "ABC"},
       "summary": "${summary}",
       "description": ${desc},
       "issuetype": {"name": "Story"},
       "assignee" : {"name":"bob"}
   }
}'

Actual Results: The variables are interpreted literally.

Expected Results: The variables should be expanded in the string.

What I've tried:

Many thanks in advance.

Upvotes: 0

Views: 1244

Answers (2)

Mike L'Angelo
Mike L'Angelo

Reputation: 880

You can build a PsObject and convert it to Json format with ConvertTo-Json. Something like this

$firstname = "myFirstName"
$lastname = "myLastName"
$properties = [ordered]@{
    firstname = $firstname
    lastname = $lastname
}
$obj = New-Object psobject -Property $properties; 
$body = convertto-json $obj


PS >$body

{
    "firstname":  "myFirstName",
    "lastname":  "myLastName"
    
}

Upvotes: 1

Theo
Theo

Reputation: 61253

There are more ways than one to do this, depending on how large the body json needs to be, you can choose to do that using

Method 1, create a double-quoted Here-String (no need to escape the other double-quotes):

$summary = 'summary'
$desc    = 'desc'
$Body    = @"
{
    "fields": {
        "project": {"key": "ABC"},
        "summary": "$summary",
        "description": "$desc",
        "issuetype": {"name": "Story"},
        "assignee" : {"name":"bob"}
    }
}
"@

Method 2: create the body as template with self-defined placeholders to be used multiple times if needed

$summary  = 'summary'
$desc     = 'desc'
$template = @'
{
    "fields": {
        "project": {"key": "ABC"},
        "summary": "@@SUMMARY@@",
        "description": "@@DESCRIPTION@@",
        "issuetype": {"name": "Story"},
        "assignee" : {"name":"bob"}
    }
}
'@

$Body = $template -replace '@@SUMMARY@@', $summary -replace '@@DESCRIPTION@@', $desc

Method 3: create numbered placeholders for use with the -f Format operator. This has the disadvantage that ALL other existing curly brackets { and } have to be doubled..

$summary  = 'summary'
$desc     = 'desc'
$template = @'
{{
    "fields": {{
        "project": {{"key": "ABC"}},
        "summary": "{0}",
        "description": "{1}",
        "issuetype": {{"name": "Story"}},
        "assignee" : {{"name":"bob"}}
    }}
}}
'@

$Body = $template -f $summary, $desc

Method 4, create the body as nested PsCustomObject and convert that to Json

$summary = 'summary'
$desc    = 'desc'
$Body = [PsCustomObject]@{
    fields = [PsCustomObject]@{
        project = [PsCustomObject]@{key = 'ABC'}
        summary = $summary
        description = $desc
        issuetype = [PsCustomObject]@{name = 'Story'}
        assignee = [PsCustomObject]@{name = 'bob'}
    }
} | ConvertTo-Json -Depth 3  # you can also set this way higher to be on the safe side

Upvotes: 4

Related Questions