当前位置: 首页 > 面试题库 >

使用Powershell正确格式化JSON

鞠隐水
2023-03-14
问题内容

我有一个JSON格式的以下文件:

之前ConvertTo-JSON

[
    {
        "Yura": {
            "Cashier": {
                "branch": "release/Retail-v4.0",
                "configuration": "RetailDemo Debug",
                "datetime_deployed": "Apr 18 2018 07:45:05",
                "deployed_by": "anonymous",
                "host": "cashier2-retail4.testing.aws.com",
                "job": "http://jenkins-testing.aws.com:8080/job/CashierDeployment",
                "lineserver": "",
                "messagebus": "",
                "product": "Cashier",
                "publish_profile": "cashier2.retail.dev.pubxml"
            },
            "ContentManager": {
                "branch": "release/Retail-v3.31.1",
                "configuration": "RetailDemo Debug",
                "datetime_deployed": "Jan 17 2018 11:59:24",
                "deployed_by": "anonymous",
                "host": "contentmanager2-retail3.testing.aws.com",
                "job": "http://jenkins-testing.aws.com:8080/job/ContentManagerDeployment",
                "lineserver": "",
                "messagebus": "",
                "product": "ContentManager",
                "publish_profile": "..\\ContentManager.PublishProfiles\\contentmanager2.retail5.dev.pubxml"
            }
        }
    }
]

使用以下代码处理数据后:

$json = Get-Content 'D:\script\test.json'  -encoding utf8 | ConvertFrom-Json
$json.yura.ContentManager.branch = 'test'

我将JSON保存到另一个文件中:

$json | convertto-json | set-content "D:\script\test1.json" -encoding utf8

问题是,保存文件后,格式损坏了:

{
    "Yura":  {
                 "Cashier":  {
                                 "branch":  "release/Retail-v4.0",
                                 "configuration":  "RetailDemo Debug",
                                 "datetime_deployed":  "Apr 18 2018 07:45:05",
                                 "deployed_by":  "anonymous",
                                 "host":  "cashier2-retail4.testing.aws.com",
                                 "job":  "http://jenkins-testing.aws.com:8080/job/CashierDeployment",
                                 "lineserver":  "",
                                 "messagebus":  "",
                                 "product":  "Cashier",
                                 "publish_profile":  "cashier2.retail.dev.pubxml"
                             },
                 "ContentManager":  {
                                        "branch":  "test",
                                        "configuration":  "RetailDemo Debug",
                                        "datetime_deployed":  "Jan 17 2018 11:59:24",
                                        "deployed_by":  "anonymous",
                                        "host":  "contentmanager2-retail3.testing.aws.com",
                                        "job":  "http://jenkins-testing.aws.com:8080/job/ContentManagerDeployment",
                                        "lineserver":  "",
                                        "messagebus":  "",
                                        "product":  "ContentManager",
                                        "publish_profile":  "..\\ContentManager.PublishProfiles\\contentmanager2.retail5.dev.pubxml"
                                    }
             }
}

我的问题是-如何在PowerShell中保留源格式?


问题答案:

由于您的原始json包含一个只有一个元素的数组,因此PowerShell会将其压缩为仅一个元素。如果在输出中希望再次将其作为数组,请使用rokumaru的好答案。

但是,PowerShell ConvertTo-Json不会生成格式精美的json,为此,我前段时间编写了一个辅助函数:

function Format-Json {
    <#
    .SYNOPSIS
        Prettifies JSON output.
    .DESCRIPTION
        Reformats a JSON string so the output looks better than what ConvertTo-Json outputs.
    .PARAMETER Json
        Required: [string] The JSON text to prettify.
    .PARAMETER Minify
        Optional: Returns the json string compressed.
    .PARAMETER Indentation
        Optional: The number of spaces (1..1024) to use for indentation. Defaults to 4.
    .PARAMETER AsArray
        Optional: If set, the output will be in the form of a string array, otherwise a single string is output.
    .EXAMPLE
        $json | ConvertTo-Json  | Format-Json -Indentation 2
    #>
    [CmdletBinding(DefaultParameterSetName = 'Prettify')]
    Param(
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
        [string]$Json,

        [Parameter(ParameterSetName = 'Minify')]
        [switch]$Minify,

        [Parameter(ParameterSetName = 'Prettify')]
        [ValidateRange(1, 1024)]
        [int]$Indentation = 4,

        [Parameter(ParameterSetName = 'Prettify')]
        [switch]$AsArray
    )

    if ($PSCmdlet.ParameterSetName -eq 'Minify') {
        return ($Json | ConvertFrom-Json) | ConvertTo-Json -Depth 100 -Compress
    }

    # If the input JSON text has been created with ConvertTo-Json -Compress
    # then we first need to reconvert it without compression
    if ($Json -notmatch '\r?\n') {
        $Json = ($Json | ConvertFrom-Json) | ConvertTo-Json -Depth 100
    }

    $indent = 0
    $regexUnlessQuoted = '(?=([^"]*"[^"]*")*[^"]*$)'

    $result = $Json -split '\r?\n' |
        ForEach-Object {
            # If the line contains a ] or } character, 
            # we need to decrement the indentation level unless it is inside quotes.
            if ($_ -match "[}\]]$regexUnlessQuoted") {
                $indent = [Math]::Max($indent - $Indentation, 0)
            }

            # Replace all colon-space combinations by ": " unless it is inside quotes.
            $line = (' ' * $indent) + ($_.TrimStart() -replace ":\s+$regexUnlessQuoted", ': ')

            # If the line contains a [ or { character, 
            # we need to increment the indentation level unless it is inside quotes.
            if ($_ -match "[\{\[]$regexUnlessQuoted") {
                $indent += $Indentation
            }

            $line
        }

    if ($AsArray) { return $result }
    return $result -Join [Environment]::NewLine
}

像这样使用它:

$json = Get-Content 'D:\script\test.json' -Encoding UTF8 | ConvertFrom-Json
$json.yura.ContentManager.branch = 'test'

# recreate the object as array, and use the -Depth parameter (your json needs 3 minimum)
ConvertTo-Json @($json) -Depth 3 | Format-Json | Set-Content "D:\script\test1.json" -Encoding UTF8

# instead of using '@($json)' you can of course also recreate the array by adding the square brackets manually:
# '[{0}{1}{0}]' -f [Environment]::NewLine, ($json | ConvertTo-Json -Depth 3) | 
#        Format-Json | Set-Content "D:\script\test1.json" -Encoding UTF8


 类似资料:
  • 这个程序的输出与预期的一样,它给出2014-12-01 17:30:15。 但是当我在iFormat中将hh替换为hh(与outputformat相同)时,它给出的输出为12Out格式2014-12-01 05:30:15 如果我将两者都转换为小写,也会发生同样的情况。为什么会出现这种类型的不一致?

  • 问题内容: 我有一些格式不一致的现有代码-有时两个空格用于缩进,有时四个空格,以此类推。代码本身是正确的,并且经过了严格的测试,但是格式却很糟糕。 在线上是否有一个地方,我可以简单地粘贴一段Python代码并自动为我缩进/设置格式?或者,是否可以执行类似的操作,并用格式化的版本覆盖每个文件? 问题答案: 编辑:如今,我建议使用autopep8,因为它不仅可以纠正缩进问题,而且(根据您的判断)可以使

  • 问题内容: 我正在尝试将JSON查询发送到Web服务,并且继续收到内部服务器错误作为对该查询的响应。 这是我要发送的内容: 这应该发送到 https://www.superService.com/api/1.7/ssapi.asmx 在准备时,使用什么方法插入行 POST /api/1.7/webservice.asmx HTTP / 1.1 ? 对象中是否包含完整的标头? JSON数据应该在对象

  • 然后我在Jsoup as中将xpath转换为CSS查询,以便稍后提取特定元素: 代码的执行没有正确定位我,而是定位到Firebug在XPath中引用的元素: 从最后来看,第一个区别是: 有没有任何替代Jsoup的方法可以处理这类问题?

  • 我正在创建一个Lua库来帮助处理发送和接收DNS请求,目前正在阅读这个(DNS协议RFC),但我不知道如何正确格式化请求。例如,我是否必须指定消息的长度?我该怎么做? 我了解,从我的Wireshark检查,我应该也包括选项之后。我还在响应中看到一个;这是否意味着我只需在添加值之前,将请求名称终止为零? 我特别谈论的部分是RFC的4.1.3。 一些注意事项:我使用个人服务器对此进行了测试,并在查询部