简体   繁体   中英

(PowerShell) How to remove double quotes from the JSON output?

Here's an excerpt from my PowerShell script, that writes JSON data to a file:

$jsonData = @"
{
    "FunctionName" :
    {
        "description": "",
        "parameters": [],
        "signature" : ""
    }
}
"@

$jsonParameterData = @"
{
    `t`t`t`t`t`t`t`t`t`t"description": "", 
    `t`t`t`t`t`t`t`t`t`t"name": ""
    `t`t`t`t`t`t`t`t`t  }
"@

$jsonData = $jsonData | ConvertFrom-Json

In the code above I have defined the JSON template that I will use to write to the file. I'm using jsonData to describe the function FunctionName with the keys of description , parameters , and signature .

On the other hand, I'm using jsonParameterData to describe each argument of the function FunctionName with the keys of description and name .

My PowerShell script pulls the data from other files, and writes this JSON information to a .json file.

For example, if the signature of the function I want to extract data from is Function FunctionName(Parameter1, Parameter2) , in the .json file I would expect to see this:

"FunctionName":  {
                      "description":  "This function does this and that",
                      "parameters":  [
                                         {
                                            "description": "This is the first parameter", 
                                            "name": "Parameter1"
                                          },
                                         {
                                            "description": "This is the second parameter", 
                                            "name": "Parameter2"
                                          }
                                     ],
                      "signature":  "Function FunctionName(Parameter1, Parameter2)"
                  }

Somewhere in the code, I store the number of function arguments in the variable NumberOfArguments ; the arguments of the function in ArrayOfArguments , and the argument descriptions in ArrayOfArgumentDescriptions .

The PowerShell code below is used to add jsonParameterData to jsonData :

for($i=0; $i -lt $NumberOfArguments; $i++){
    $SelectedArgument = $ArrayOfArguments[$i]
    $SelectedDescription = $ArrayOfArgumentDescriptions[$i]
    $jsonData.AplusWait.parameters += $jsonParameterData
}

Then, I write jsonData to the file like this:

$jsonData = $jsonData | ConvertTo-Json | ForEach-Object { [System.Text.RegularExpressions.Regex]::Unescape($_) }
$jsonData.Substring(1, $jsonData.length - 2) | Add-Content -Path $OutputFile
Write-Output "`nFinished writing JSON data to file.`n"

However, the resulting output slightly does not match the intended output. The output looks like this:

"FunctionName":  {
                      "description":  "This function does this and that",
                      "parameters":  [
                                         "{
                                            "description": "This is the first parameter", 
                                            "name": "Parameter1"
                                          }",
                                         "{
                                            "description": "This is the second parameter", 
                                            "name": "Parameter2"
                                          }"
                                     ],
                      "signature":  "Function FunctionName(Parameter1, Parameter2)"
                  }

If you look closely, the output displays the parameters array as an array of strings (note the double quotes), when it should actually be an array of JSON objects.

How can I transform this:

"{
    "description": "This is the first parameter", 
    "name": "Parameter1"
}"

into this:

{
    "description": "This is the first parameter", 
    "name": "Parameter1"
}

?

Having the following JSONs:

$jsonData = @"
{
  "foo": {
    "des": "foo-des",
    "par": [],
    "sig": "foo-sig"
  }
}
"@

$jsonParamData = @"
{
  "des": "param-des",
  "name": "param-name"
}
"@

$jsonObj = $jsonData | ConvertFrom-Json
$jsonParamObj = $jsonParamData | ConvertFrom-Json

# add param
$jsonObj.foo.par += $jsonParamObj

# Pay attention to the -Depth parameter otherwise the final json will be truncated
$jsonResult = $jsonObj | ConvertTo-Json -Depth 5
Write-Host $jsonResult

Should print:

{
  "foo": {
    "des": "foo-des",
    "par": [
      {
        "des": "param-des",
        "name": "param-name"
      }
    ],
    "sig": "foo-sig"
  }
}

When converting objects back to json you should pay attention to the -Depth parameter since it will truncate your result.

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