PowerShell JSON添加值格式 [英] PowerShell JSON adding value format

查看:132
本文介绍了PowerShell JSON添加值格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将数据添加到json文件中.我这样做是

I am adding data to a json file. I do this by

$blockcvalue =@" 
    {
    "connectionString":"server=(localdb)\\mssqllocaldb; Integrated Security=true;Database=$database;"
    }
"@

$ConfigJson = Get-Content C:\Users\user\Desktop\myJsonFile.json -raw | ConvertFrom-Json

$ConfigJson.data | add-member -Name "database" -value (Convertfrom-Json $blockcvalue) -MemberType NoteProperty

$ConfigJson | ConvertTo-Json| Set-Content C:\Users\user\Desktop\myJsonFile.json

但是格式如下:

{
    "data":  {
                    "database":  {
                                 "connectionString":  "server=(localdb)\\mssqllocaldb; Integrated Security=true;Database=mydatabase;"
                             }
                }
}

但是我需要这样:

{
    "data": {
    "database":"server=(localdb)\\mssqllocaldb; Integrated Security=true;Database=mydatabase;"
    }
    }
}

有人可以帮忙吗?

推荐答案

这是我用来美化JSON输出的功能:

Here's my function to prettify JSON output:

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 Indentation
        Optional: The number of spaces to use for indentation. Defaults to 2.
    .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 4
    #>
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
        [string]$Json,

        [int]$Indentation = 2,
        [switch]$AsArray
    )
    # 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
    $Indentation = [Math]::Abs($Indentation)
    $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
}

像这样使用它:

$ConfigJson | ConvertTo-Json | Format-Json | Set-Content C:\Users\user\Desktop\myJsonFile.json

这篇关于PowerShell JSON添加值格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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