【问题标题】:Is there a way to split json data from a file in powershell?有没有办法从powershell中的文件中拆分json数据?
【发布时间】:2019-04-05 15:33:00
【问题描述】:

我正在通过 powershell 从 json 文件中读取数据,最终目标是更新所述文件。我需要将数据拆分为要保留的块和要更新的块,并且为了使事情进一步复杂化,我需要拆分文本的位置在整个 foreach 循环中会有所不同,因此我需要该部分来自一个变量。

我在多种配置中尝试过.split/-split.replace/-replace,但似乎这比在 powershell 中假设的要难。

以下所有文件都在同一个文件夹中。

Json (json.json):

{
  "Section1": {
    "Heading1": [
      "Thing1",
      "Thing2"
    ]
  },
  "Section2": [
    "Thing1",
    "Thing2"
  ]
}

Powershell (powershell.ps1):

$originalJsonString = Get-Content -Path ".\json.json"
$SplitTarget = "Section2"

$JsonString = {This is the part that I am iffy about}

Write-Output $JsonString

我想从上面得到的输出是

{
"Section1": {
    "Heading1": [
        "Thing1",
        "Thing2"
    ]
},

我已经尝试了几乎所有我能想到的与拆分和替换相关的方法,但解决方案却暗示了我。 请注意,在上面的解决方案中,$originalJsonString 被 $SplitTarget 分割(或其他)是一个重要因素,而不是“Section2”,因为这也是我等式中的一个因素。

这是我第一次问,所以如果我做错了什么,我道歉。

谢谢。

编辑: 我添加不转换为对象的原因是公平的。 将 json 转换为对象并返回时 powershell 导出的语法不适合我使用。 但是,如果使用对象是唯一的方法,并且分切是不可能的,那么必须找到另一种解决方案。 谢谢。

编辑: 如果对象是要走的路,我最终找到了一种更复杂的方法来按照我想要的方式格式化 .json。 谢谢大家。

【问题讨论】:

  • 您可以摆弄-split 正则表达式,但实际上是 IMO 转换为对象的唯一可靠方法。毕竟... JSON JavaScript Object Notation。

标签: json powershell replace split


【解决方案1】:

你应该做的是在 using 中读取 json 文件:

$json = Get-Content .\json.json -raw | ConvertFrom-Json

我在这里使用'here-string'伪造了它:

$json = @"
{
    "Section1": {
        "Heading1": [
            "Thing1",
            "Thing2"
        ]
    },
    "Section2": [
        "Thing1",
        "Thing2"
    ]
}
"@ | ConvertFrom-Json

接下来,定义您要保留或更新的内容:

$sectionToKeep   = "Section1"
$sectionToUpdate = "Section2"

要查看里面有什么,请使用$json.$sectionToKeep | ConvertTo-Json

{
    "Heading1":  [
                     "Thing1",
                     "Thing2"
                 ]
}

接下来,更新 $section2 保留其他所有内容。我正在编写一个存储数组的对象,就像在$sectionToKeep

$json.$sectionToUpdate = @{'Heading2' = 'Thing3', 'Thing4'}

最后输出(或写回文件)新的完整json:

$json | ConvertTo-Json

用你的例子给你这个:

{
    "Section1":  {
                     "Heading1":  [
                                      "Thing1",
                                      "Thing2"
                                  ]
                 },
    "Section2":  {
                     "Heading2":  [
                                      "Thing3",
                                      "Thing4"
                                  ]
                 }
}

希望有帮助

【讨论】:

  • 你是对的,我的 json 很糟糕。我一定会马上解决的,谢谢。编辑:有人已经这样做了;P
  • @Graesholt。做得好。我已从答案中删除了该评论
【解决方案2】:

不确定是否真正理解您的问题,但我会将 Json 转换为对象,然后过滤子数据并再次创建文件

$obj = Get-Content json.json -raw | ConvertFrom-Json

【讨论】:

    【解决方案3】:

    如果您只想隔离一个部分并创建一个新的 json 文件,那么您可以使用以下内容:

    $file = Get-Content .\json.json | ConvertFrom-Json
    $file | Select-Object Section1 | ConvertTo-Json | Set-Content New.json
    

    【讨论】:

      猜你喜欢
      • 2022-01-11
      • 2020-09-28
      • 2010-11-29
      • 2021-04-02
      • 1970-01-01
      • 2023-02-16
      • 2022-12-18
      • 2021-11-01
      • 1970-01-01
      相关资源
      最近更新 更多