4

I am quite new to powershell stuff. So need some help here.

I want to pass a json object as a parameter to another ps1. From what I read after searching is that I need to convert it to powershell object from json string. Please correct me if I am wrong. This is what I am doing

Calling script:

$jsonParams = "{
     `"TaskName`": `"$taskName`",
      `"ExitCode`": `"$exitCode`",
      `"ErrorMessage`": `"$errorMessage`"
   }

$jsonObject = $jsonParams | ConvertFrom-Json
$argumentList = @($param1, $param2, $jsonObject) 

Invoke-Expression "& `"$scriptPath`" $argumentList"

and in called script -

param (
    [string]$param1,
    [string]$param2,
    [Microsoft.PowerShell.Commands.JsonObject]$jsonObject
)

But, the calling script throws error

ConvertFrom-Json : Invalid object passed in, ':' or '}' expected. (21): {

What's wrong with this code. Also, after json object is passed to called script, how should I access its values in it.

Thanks!!

0

1 Answer 1

6

Your JSON is malformed. I think the core issue is that you have a trailing comma at the end of your JSON. You also don't close the opening quotation in your declaration.

You might have a much easier time if you use a here-string for this anyway. This was you don't have to use all those backticks.

$jsonParams = @"
{
     "TaskName": "$taskName",
      "ExitCode": "$exitCode",
      "ErrorMessage": "$errorMessage"
   }
"@

$jsonObject = $jsonParams | ConvertFrom-Json

$jsonObject is already a custom object and no longer JSON. You don't need to do anything special with it. Remove the type in your param block and just call the properties in your script.

param (
    [string]$param1,
    [string]$param2,
    $jsonObject
)
$jsonObject.TaskName
Sign up to request clarification or add additional context in comments.

5 Comments

This is only a partial answer as you will still get an error with that type cast. I don't use that so I am not sure what the issue is there other than the text is no longer JSON as you converted it.
That was just typo while putting the question, Actually there are more parameters in my code. So I copy-pasted few of them. Also, the quotes are properly closed too. As shown in your code, I added the here-string header but same is the error again :(
Can you show me an example of what you are passing to the variables that make the json? You could print the herestring or your string into jsonlint.com to see where the issue is with it. You have a syntax error that you are hiding I feel now.
Ok, sorry my bad. There was some problem while reading the ini file values which were forming my json string. I fixed that and checked on the above link and it showed me it is valid json. Also, that error is gone now but if I try to access json values in destination file with your code, it prints nothing.
Fyi.. @{TaskName=setup; ExitCode=1; ErrorMessage=NoError;} This is the output after ConvertFrom-Json is executed

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.