【问题标题】:Powershell JSON data group byPowershell JSON 数据分组方式
【发布时间】:2022-11-02 01:20:49
【问题描述】:

我正在开发自动化来触发电子邮件,我需要向从 JSON 文件读取的每个电子邮件 ID(TestValue3)发送一封电子邮件,并且电子邮件的正文应该包含分别对应于电子邮件 ID 的 TestValue1、TestValue2 和 TestValue4 的值.

在这里,相同的电子邮件 ID 不止一次出现,因此我需要将各个电子邮件 ID(重复)的所有信息分组,而不是发送多封电子邮件,并将其作为一封电子邮件发送。

{
"Data":  [
             {
                 "TestValue1":  "MyCode35",
                 "TestValue2":  "Some Test Value1",
                 "TestValue3":  "user122@gmail.com",
                 "TestValue4":  "Account1"
             },
             {
                 "TestValue1":  "MyCode38",
                 "TestValue2":  "Some Test Value2",
                 "TestValue3":  "user121@gmail.com",
                 "TestValue4":  "Account2"
             },
             {
                 "TestValue1":  "MyCode56",
                 "TestValue2":  "Some Test Value2",
                 "TestValue3":  "user121@gmail.com",
                 "TestValue4":  "Account3"
             },
             {
                 "TestValue1":  "MyCode35",
                 "TestValue2":  "Some Test Value1",
                 "TestValue3":  "user1234@gmail.com",
                 "TestValue4":  "Account8"
             },
             {
                 "TestValue1":  "MyCode35",
                 "TestValue2":  "Some Test Value1",
                 "TestValue3":  "abc.user1234@gmail.com",
                 "TestValue4":  "Account11"
             },
             {
                 "TestValue1":  "MyCode88",
                 "TestValue2":  "Some Test Value1",
                 "TestValue3":  "user1234@gmail.com",
                 "TestValue4":  "Account32"
             },
             {
                 "TestValue1":  "MyCode45",
                 "TestValue2":  "Some Test Value1",
                 "TestValue3":  "user122@gmail.com",
                 "TestValue4":  "Account18"
             }
        ]
}

更新代码后,它返回分组数据。

$json = Get-Content -Raw -Path .\filename.json | ConvertFrom-Json
$json | ForEach-Object Data | Group-Object -Property TestValue3

示例电子邮件正文,我将在其中使用这些 TestValue1、TestValue2 和 TestValue4

用户详情

代码是:$TestValue1
测试值是:$TestValue2
帐户是:$TestValue4

有什么想法或建议吗?

【问题讨论】:

  • 文档根对象只有一个属性"Data"。你会想做Get-Content -Raw -Path \filename.json | ConvertFrom-Json | ForEach-Object Data | Group-Object -Property TestValue3
  • 为什么任何分组元素都具有Type 属性? JSON 中唯一的属性名称是 TestValue1TestValue2TestValue3TestValue4 :)
  • 是的,我错过了“数据”属性。现在我明白了,但是当我尝试对其他属性值的底层数据进行分组时,它变得空了。下面是代码$json | ForEach-Object Data | Group-Object -Property TestValue3 | ForEach{@{$_.Name = $_.Group.TestValue2}} | ConvertTo-Json $json 具有 json 内容 `` $json = Get-Content -Raw -Path .\filename.json | ConvertFrom-Json `.更新了我的评论。这仅获取 TestValue2 因此需要获取该 useremail ID 的所有值
  • update your question 提供您遇到问题的代码,cmets 不太适合 :)
  • 更新了我原来的帖子:)

标签: powershell


【解决方案1】:

更改数据以更好地说明用法...

$json = @'
{
    "Data": [
                {
                    "Codes":      "MyCode35",
                    "TestValue":  "Some Test Value1",
                    "Recipient":  "user122@gmail.com",
                    "Account":    "Account1"
                },
                {
                    "Codes":      "MyCode38",
                    "TestValue":  "Some Test Value2",
                    "Recipient":  "user121@gmail.com",
                    "Account":    "Account2"
                },
                {
                    "Codes":      "MyCode56",
                    "TestValue":  "Some Test Value2",
                    "Recipient":  "user121@gmail.com",
                    "Account":    "Account3"
                },
                {
                    "Codes":      "MyCode35",
                    "TestValue":  "Some Test Value1",
                    "Recipient":  "user1234@gmail.com",
                    "Account":    "Account8"
                },
                {
                    "Codes":      "MyCode35",
                    "TestValue":  "Some Test Value1",
                    "Recipient":  "abc.user1234@gmail.com",
                    "Account":    "Account11"
                },
                {
                    "Codes":      "MyCode88",
                    "TestValue":  "Some Test Value1",
                    "Recipient":  "user1234@gmail.com",
                    "Account":    "Account32"
                },
                {
                    "Codes":      "MyCode45",
                    "TestValue":  "Some Test Value1",
                    "Recipient":  "user122@gmail.com",
                    "Account":    "Account18"
                }
            ]
}
'@

分组和遍历...

$SmtpGateway = '127.0.0.1'

$GroupedRecipents = $json | ConvertFrom-Json | select -ExpandProperty Data | group Recipient

foreach ($GroupItem in $GroupedRecipents){
    $body = $GroupItem.Group | Select * -ExcludeProperty Recipient | ConvertTo-Html -Fragment
    Send-MailMessage `
        -From 'sender@nowhere.out' `
        -SmtpServer $SmtpGateway `
        -To $GroupItem.Name `
        -Subject 'Here is the mail' `
        -BodyAsHtml "$body"
}

【讨论】:

  • 谢谢@丹尼斯。它为我提供了您的代码的映射值。在我的脚本中,我需要使用分组值作为变量的值。那么我现在如何从组中获取值。我试过$json | select -ExpandProperty Data | group TestValue3 | ForEach{@{$_.Name = $_.Group.TestValue1}},它给出了名称值对。
  • 您能否更新您最初的问题,即您需要转发哪些数据以及所需的格式应该是什么?分组主要用于漂亮的输出或旋转。
  • 更新了原帖
  • 更新了答案...
猜你喜欢
  • 2020-08-19
  • 2020-07-24
  • 1970-01-01
  • 2015-07-29
  • 2012-04-24
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多