PowerShell JSON 添加值格式

2024-05-22

我正在向 json 文件添加数据。我这样做是通过

$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 输出的函数:

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PowerShell JSON 添加值格式 的相关文章

  • AWS SES模板html部分是多行

    我正在使用 AWS SES 按照文档发送电子邮件https docs aws amazon com ses latest DeveloperGuide send personalized email api html https docs
  • Play Framework 2.3 (Scala) 中的自定义 JSON 验证约束

    我设法使用自定义约束实现表单验证 但现在我想对 JSON 数据执行相同的操作 如何将自定义验证规则应用于 JSON 解析器 示例 客户端的 POST 请求包含用户名 username 我不仅要确保该参数是非空文本 而且还要确保该用户确实存在
  • 如何更改 Visual Studio Code“Powershell 集成控制台”?

    有谁知道如何安装 更新 PS 7 以供 VS Code Powershell 集成控制台使用 我可以在常规 powershell 终端上获取 PS 7 但 Powershell 集成控制台仍然是 PSVersion 5 1 我似乎不知道如何
  • 使用 JSON.NET 将 JSON 数据反序列化为 C#

    我对使用 C 和 JSON 数据比较陌生 正在寻求指导 我使用的是 C 3 0 NET3 5SP1 和 JSON NET 3 5r6 我有一个已定义的 C 类 需要从 JSON 结构填充该类 但是 并非从 Web 服务检索的条目的每个 JS
  • 通过powershell脚本在WSL中运行bash脚本

    我正在尝试运行一个启动 WSL ubuntu1804 终端的脚本 然后在该终端中运行 bash 脚本 ubuntu1804 exe cd test directory node server js 然而 在第一个命令之后 终端打开 其他两个
  • 使用 Jade 评估自定义 javascript 方法 (CircularJSON)

    我想通过 Jade 将一个对象解析为客户端 JavaScript 通常这会起作用 script var object JSON parse JSON stringify object but my object is circular ht
  • Azure 数据工厂 - 从 Data Lake Gen 2 JSON 文件中提取信息

    我有一个 ADF 管道 将原始日志数据作为 JSON 文件加载到 Data Lake Gen 2 容器中 我们现在想要从这些 JSON 文件中提取信息 我正在尝试找到从所述文件中获取信息的最佳方法 我发现 Azure Data Lake A
  • 有没有更快的方法来使用Powershell解析Excel文档?

    我正在与一个接口MS Excel文件通过Powershell 每个 Excel 文档可能有大约 1000 行数据 目前这个脚本似乎读取了Excel文件并以每 0 6 秒 1 条记录的速率将值写入屏幕 乍一看 这似乎非常慢 这是我第一次阅读E
  • 使用 JaxRS 自定义 JSON 序列化

    在 Web 服务调用中 我想返回具有此 JSON 结构的对象 date 30 06 2014 price val 12 50 curr EUR 我想将此 JSON 代码映射到此 Java 结构 使用乔达时间 http www joda or
  • Powershell:输出文件

    我正在尝试将所有控制台输出记录到文件中 移动项目 D scripts fileA D scripts fileB verbose Force 输出文件 D scripts move log 追加 如果该文件不存在 则正在创建该文件 但详细信
  • JavaScript 相对路径

    在第一个 html 文件中 我使用了一个变量类别链接 var categoryLinks Career prospects http localhost Landa DirectManagers 511 HelenaChechik Dim0
  • 如何使用jq将JSON对象流转换为数组

    我想用jq将 json 对象流放入 json 数组中 例如 来自 a 1 b 2 to a 1 b 2 但这是行不通的 echo a 1 b 2 jq 自从我得到 a 1 b 2 用吸吮它 s option jq s lt lt lt a
  • 将 `new Date ()` 存储在 JSON 对象中

    我有以下字段验证器对象 type date min new Date 我希望我可以存储new Date 作为 JSON 中的表达式 解析时会执行 保存时间戳 type date min new Date getTime 然后你再读一遍 va
  • 如何在 Go 中从 stdin 解析无限 json 数组?

    我正在尝试编写一个 i3status 的小替代品 一个与 i3bar 兼容的小程序进行通信this http i3wm org docs i3bar protocol html协议 他们通过标准输入和标准输出交换消息 两个方向的流都是一个无
  • 如何将 PowerShell cmdlet 或函数添加到我的计算机以使其始终可用?

    如果我找到 或创建 新的 PowerShell cmdlet 或函数 如何将其添加到我的计算机 我是否将其复制到特定文件夹 我是否将其内容放入特定文件中 我是否需要授权 签名或以某种方式给予许可 我不想只在一次会话中使用它 我希望每当我在此
  • JSON Patch RFC 中的波浪号转义应该如何操作?

    参考文献https www rfc editor org rfc rfc6902 appendix A 14 https www rfc editor org rfc rfc6902 appendix A 14 A 14 逃脱命令 目标 J
  • MySQL 8 用逗号分割字符串并将其转换为JSON ARRAY

    我有以下字符串 a b c d 我想将它转换成一个 json 数组 像这样 a b c d MySQL 8 有什么函数可以实现这个功能吗 Try SELECT CAST CONCAT REPLACE a b c d AS JSON See
  • Android - 下载 JSON 数据并保存到共享首选项

    我正在从 PHP 服务读取 JSON 数据 每当该 JSON 的版本发生变化时 我想将其存储在 Android 上 用新数据替换旧数据 JSON 仅用于填充 Spinner 我的问题是 JSON 有 36KB 可以将其存储在共享首选项中有一
  • Struts2 中有多种结果类型?

    我有一个使用 Tiles 的 Struts2 应用程序 如何在操作映射中获取多种结果类型 因为我需要将de输出设置为JSON数据 并且同时Tiles 我努力了
  • VSTS部署IIS应用程序winrm并更改appsettings.json

    我正在使用 部署 IIS 应用程序 winrm 任务在另一台计算机上部署 IIS 应用程序 此任务部署 zip 文件 在此 zip 中有一个 appsettings json 其变量以下划线开头和结尾 我需要替换每个环境的 appsetti

随机推荐