powershell"ConvertTo-Json"混乱了json格式的输出 [英] powershell "ConvertTo-Json" has messed json format output

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

问题描述

1)$ Getjsonfilecontent =获取内容"C:\ Scripts \ CreateADF-Datasets \ BUSI_RULE_BUILD_LOCATION_CODES_Source_Def.json"-原始| 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-深度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的一部分,其中包括键对值,但该部分没有经过udp处理,应在转换之前和之后保持不变. 我们对此表示任何帮助.

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格式的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆