将多个JSON文件读取到Powershell对象数组中,并过滤出具有相同值的属性 [英] Read multiple JSON files into an array of Powershell objects and filter out those with the same value for a property

查看:151
本文介绍了将多个JSON文件读取到Powershell对象数组中,并过滤出具有相同值的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一次,所以让我知道我在问题的布局上做错了什么.

It's my first time, so let me know if I'm doing something wrong with the layout of my question.

我有很多JSON文件,它们的文件名遵循命名约定,即file1.json,file2.json等.每个文件都可能有多个看起来像这样的对象:

I have a lot of JSON files with filenames that follow a naming convention, i.e. file1.json, file2.json, etc. Each of which are likely to have multiple objects which look like:

[
    {
        "Forename":  "Jim",
        "Surname":  "Cook",
        "Gender":  "M",
        "DOB":  "12-03-1994"
    },
    {
        "Forename":  "Sarah",
        "Surname":  "Parker",
        "Gender":  "F",
        "DOB":  "01-02-1983"
    },
    {
        "Forename":  "Alan",
        "Surname":  "Flemming",
        "Gender":  "M",
        "DOB":  "27-10-1989"
    }
]

在Powershell中,我想将这些JSON对象转换为Powershell对象,然后为属性选择具有相同值的对象,例如名字叫"Jim"的人.

In Powershell, I would like to convert these JSON objects into Powershell objects and then select objects with the same value for a property, like people whose first name is "Jim".

到目前为止,我已经实现了这一点:

So far I've achieved this:

@(Get-ChildItem "file*.json" | %{Get-Content $_.FullName | Out-String | ConvertFrom-Json}) | Where-Object {$_.Forename -eq "Jim"}

只有一个文件可使用时,此方法有效,输出:

This works when there is only one file to work with, which outputs:

Forename Surname Gender DOB
-------- ------- ------ ---
Jim      Cook    M      12-03-1994

但是,当与多个文件一起使用时,它会失败并输出所有对象,就像忽略了Where-Object一样.结果看起来像这样:

However it fails and outputs all objects when used with multiple files, as though the Where-Object is being ignored. The result can look like this:

Forename Surname  Gender DOB
-------- -------  ------ ---
Jim      Cook     M      12-03-1994
Sarah    Parker   F      01-02-1983
Alan     Flemming M      27-10-1989
Bill     Preston  M      04-07-1975
Helen    Smith    F      03-12-2001

有人可以建议我在这里做错了什么,如何解决它才能得到正确的结果?谢谢

Can someone please suggest what I'm doing wrong here and how it can be fixed to get the correct result? Thanks

推荐答案

问题是 ConvertFrom-Json将JSON数组(从-转换)输出为单个对象而不是逐个元素,就像在PowerShell中通常那样.

The problem is that ConvertFrom-Json outputs (converted-from-)JSON arrays as single objects rather than element by element, as is otherwise typical in PowerShell.

此有问题的行为是此GitHub问题的主题. /sup>

This problematic behavior is the subject of this GitHub issue.

如果至少一个元素 具有值为Jim.ForeName属性,则导致整个数组Where-Object获取输出,感谢成员枚举.

This results in the entire array getting output by Where-Object if (at least) one of its elements has a .ForeName property with value Jim, thanks to member enumeration.

解决方法是强制枚举,在最简单的情况下,是通过将ConvertFrom-Json调用命令包装在(...)中来实现的:

The workaround is to force enumeration, which in the simplest case is achieved by wrapping the command the ConvertFrom-Json call in (...):

Get-ChildItem file*.json | ForEach-Object {
  (Get-Content -Raw $_.FullName | ConvertFrom-Json)
} | Where-Object { $_.Forename -eq "Jim" }

请注意,我已将Get-Content $_.FullName | Out-String替换为Get-Content -Raw $_.FullName(PSv3 +),这对于以单个,多行字符串的形式检索文件内容既简洁又有效.

Note that I've replaced Get-Content $_.FullName | Out-String with Get-Content -Raw $_.FullName (PSv3+), which is both more concise and more efficient for retrieving a file's content as a single, multi-line string.

这篇关于将多个JSON文件读取到Powershell对象数组中,并过滤出具有相同值的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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