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!!

Saurabh
  • 157
  • 1
  • 3
  • 11

1 Answers1

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
Matt
  • 45,022
  • 8
  • 78
  • 119
  • 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. – Matt Dec 08 '15 at 20:49
  • 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 :( – Saurabh Dec 08 '15 at 21:22
  • 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 http://jsonlint.com/ to see where the issue is with it. You have a syntax error that you are hiding I feel now. – Matt Dec 08 '15 at 21:40
  • 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. – Saurabh Dec 08 '15 at 22:16
  • Fyi.. `@{TaskName=setup; ExitCode=1; ErrorMessage=NoError;}` This is the output after `ConvertFrom-Json` is executed – Saurabh Dec 08 '15 at 22:25