0

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"
      }
     }
     ]
     }'

2 Answers 2

3

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"
        }
    }]
}
Sign up to request clarification or add additional context in comments.

Comments

3

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.

1 Comment

I'd also mention to add -Depth so that the hashtable under customfield is properly converted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.