powershell “ConvertTo-Json” has messed json format

2019-08-26 20:32发布

问题:

1) $Getjsonfilecontent = Get-Content "C:\Scripts\CreateADF-Datasets\BUSI_RULE_BUILD_LOCATION_CODES_Source_Def.json" -Raw | ConvertFrom-Json

2) Append some key pair values to the json file

3) $Getjsonfilecontent | ConvertTo-Json -Depth 100 | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } | set-content $Updatedleafjsonpath (Updatedleafjsonpath is a copy of base file 'Getjsonfilecontent')

when i do this, am seeing some format is messed from "$Updatedleafjsonpath" as shown below. All of the "\" are missing and "\n" is also messed in my output. Note: There is another section in same json file that is udpated with key pair values as part of step2, but this sections is not udpated and should remain as is before and after conversion. Any help on this is really appreciated.

Input: same output is expected.
 "typeProperties": {
                "format": {
                    "type": "TextFormat",
                    "columnDelimiter": "|",
                    "rowDelimiter": "\n",
                    "quoteChar": "\"",
                    "nullValue": "\"\"",
                    "encodingName": null,
                    "treatEmptyAsNull": true,
                    "skipLineCount": 0,
                    "firstRowAsHeader": false
                },
                "fileName": "[parameters('Veh_Obj_properties_typeProperties_fileName')]",
                "folderPath": "[parameters('Veh_Obj_properties_typeProperties_folderPath')]"
            }
                                             }

What i got after conversion:

"typeProperties":  {
                                                                "format":  {
                                                                               "type":  "TextFormat",
                                                                               "columnDelimiter":  "|",
                                                                               "rowDelimiter":  "
",
                                                                               "quoteChar":  """,
                                                                               "nullValue":  """",
                                                                               "encodingName":  null,
                                                                               "treatEmptyAsNull":  true,
                                                                               "skipLineCount":  0,
                                                                               "firstRowAsHeader":  false
                                                                           },
                                                                "fileName":  "[parameters('Veh_Obj_properties_typeProperties_fileName')]",
                                                                "folderPath":  "[parameters('Veh_Obj_properties_typeProperties_folderPath')]"
                                                            }

回答1:

never mind, got the answer.. added some more lines to code to make it work
$Getjsonfilecontent | ConvertTo-Json -Depth 100 | Out-File $Updatedleafjsonpath -Force
     # Remove unwanted Pattern
    $ReplacePatterns = @{
    "\\u003c" = "<"
    "\\u003e" = ">"
    "\\u0027" = "'"
    }
    $InputJson = Get-Content -Path $Getjsonfilecontent | Out-String
    foreach ($Pattern in $ReplacePatterns.GetEnumerator())
    {
        $InputJson = $InputJson -replace $Pattern.Key, $Pattern.Value
    }
    $InputJson | Out-File -FilePath $Updatedleafjsonpath