弄乱了json格式输出

弄乱了json格式输出

本文介绍了powershell“ConvertTo-Json"弄乱了json格式输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

2) 将一些密钥对值附加到 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 是基础文件Getjsonfilecontent"的副本)

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

当我这样做时,我看到某些格式与$Updatedleafjsonpath"混淆,如下所示.所有的\"都丢失了,\n"也在我的输出中被弄乱了.注意:在同一个 json 文件中还有另一部分使用密钥对值进行了更新,作为步骤 2 的一部分,但该部分未更新,转换前后应保持原样.非常感谢您对此的任何帮助.

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

转换后我得到了什么:

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

推荐答案

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

这篇关于powershell“ConvertTo-Json"弄乱了json格式输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 08:18