【问题标题】:Powershell joining 2 different JSON filesPowershell加入2个不同的JSON文件
【发布时间】:2020-04-23 03:34:20
【问题描述】:

我有 2 个 JSON 文件,我想合并它们以提取一些有意义的数据。第一个文件包含组和详细信息。第二个文件包含附加到组的记录列表。

文件 1:

{
    "count":  1,
    "results":  [
                    {
                        "key":  "card_sets",
                        "id":  "551175"
                    }
                ],
    "card_sets":  {
                           "551175":  {
                                          "title":  "Title",
                                          "account_default":  false,
                                          "active_currencies":  "EUR",
                                          "default_currencies":  "EUR GBP",
                                          "destroyable":  true,
                                          "created_at":  "2020-04-20T08:09:15-07:00",
                                          "updated_at":  "2020-04-20T08:09:15-07:00",
                                          "id":  "551175"
                                      }
                       },
    "meta":  {
                 "count":  1,
                 "page_count":  1,
                 "page_number":  1,
                 "page_size":  200
             }
}

文件 2:

{
    "count":  1,
    "results":  [
                    {
                        "key":  "cards",
                        "id":  "847355"
                    }
                ],
    "cards":  {
                       "847355":  {
                                      "currency":  "EUR",
                                      "created_at":  "2020-04-20T08:09:15-07:00",
                                      "updated_at":  "2020-04-20T08:09:15-07:00",
                                      "card_set_id":  "551175",
                                      "id":  "847355"
                                  }
                   },
    "meta":  {
                 "count":  1,
                 "page_count":  1,
                 "page_number":  1,
                 "page_size":  200
             }
}

为了清楚起见,我减少了输出。

我想要实现的是将这两个具有相应 ID 的文件连接在一起。

文件 1 的密钥是 card_sets.{id}.id

文件 2 的密钥是 卡片。{id}.card_set_id

我并不真正关心 count/results/meta 元素中的任何结果,但我希望汇总一个看起来像的列表

File1.id、File1.title、File1.active_currencies、File2.id、File2.currency

我只使用了 PowerShell 一天半,刚刚获取 JSON 文件对我来说是一大步,但到目前为止我能找到的所有谷歌搜索都是基于相同的 json 文件合并到一。这似乎有点复杂,我现在很难过。

有人可以帮忙吗?

【问题讨论】:

标签: json powershell join


【解决方案1】:

您可以尝试以下方法:

$json1 = Get-Content file1.json | ConvertFrom-Json
$json2 = Get-Content file2.json | ConvertFrom-Json
$cardsets = $json1.card_sets | Select-Object -Expand *
$cards = $json2.cards | Select-Object -Expand *

foreach ($cardset in $cardsets) {
    $card = $cards | Where card_set_id -eq $cardset.id
    if ($cardset.id -in $cards.card_set_id) {
        [pscustomobject]@{
            'File1.id' = $cardset.id
            'File1.title' = $cardset.title
            'File1.active_currencies' = $cardset.active_currencies
            'File2.id' = $card.id
            'File2.currency' = $card.currency
        }
    }
}

【讨论】:

  • 谢谢!我目前收到错误“无法扩展多个属性”,但你已经给了我至少开始取得更多进展所需的东西。如果您提供的内容使我得到正确的答案,我将使其正确。
  • 我解决了“无法扩展多个属性”错误,方法是分解两个 json 文件,然后使用您的连接将它们组合在一起。感谢您的帮助!
猜你喜欢
  • 2022-07-26
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多