简体   繁体   中英

How do I pass variables through a Json body Powershell

I want to pass variables in for these values but I cant get them to go for example in user_id I want to pass the variable $userID This is an example of the Body i'm using:

$body = '{

    "data":
    [
    {
     "user_id":$userID,
     "type":"manual",
     "date":"2021-01-30",
     "duration":"150",
     "jobcode_id":"15281216",
     "notes":"This is a test of a manual time entry",
     "customfields": {
      "54138" : "IT Services",
      "54136" : "Yes"
      }
     }
     ]
     }'

I would use a double-quoted Here-String for that:

$userID = 'Alex'
$body = @"
{
    "data": [{
        "user_id": "$userID",
        "type": "manual",
        "date": "2021-01-30",
        "duration": "150",
        "jobcode_id": "15281216",
        "notes": "This is a test of a manual time entry",
        "customfields": {
            "54138": "IT Services",
            "54136": "Yes"
        }
    }]
}
"@

$body now contains:

{
    "data": [{
        "user_id": "Alex",
        "type": "manual",
        "date": "2021-01-30",
        "duration": "150",
        "jobcode_id": "15281216",
        "notes": "This is a test of a manual time entry",
        "customfields": {
            "54138": "IT Services",
            "54136": "Yes"
        }
    }]
}

The reason why $userID is not substituted for it's value in your example is because you are using a single quote ( ' ) character. PowerShell only substitutes when you use a double quote ( " ) character.

This would give you the challenge that your data already contains double quotes. There Here string from Theo's answers works just fine but as a personal preference I would use a PowerShell hashtable to construct an object and convert it to Json using Convert-To-Json .

Example:

$userID = 'John'
$body = @{
    "data" = ,@{
        "user_id" = $userID;
        "type" = "manual";
        "date" = "2021-01-30";
        "duration" = "150";
        "jobcode_id" = "15281216";
        "notes" = "This is a test of a manual time entry";
        "customfield" = @{
            "54138" = "IT Services";
            "54136" = "Yes";
        }   
    }
}

$body | ConvertTo-Json -Depth 3

Output:

{
    "data":  [
                 {
                     "notes":  "This is a test of a manual time entry",
                     "customfield":  {
                                         "54138":  "IT Services",
                                         "54136":  "Yes"
                                     },
                     "duration":  "150",
                     "type":  "manual",
                     "date":  "2021-01-30",
                     "jobcode_id":  "15281216",
                     "user_id":  "John"
                 }
             ]
}

EDIT: As robdy mentioned in the comments, the Depth parameter should be used (I've added it). A good explanation can be found here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM