[英]Powershell joining 2 different JSON files
I have 2 JSON files that I would like to combine in order to pull out some meaningful data.我有 2 个 JSON 文件,我想合并它们以提取一些有意义的数据。 The first file contains groups and details.第一个文件包含组和详细信息。 The second file contains a list of records that are attached to the group.第二个文件包含附加到组的记录列表。
File 1:文件 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
}
}
File 2:文件 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
}
}
For the sake of clarity I have reduced the output.为了清楚起见,我减少了 output。
What I want to achieve is to join these 2 files together where they have corresponding IDs.我想要实现的是将这两个文件连接在一起,它们具有相应的 ID。
File 1 key would be card_sets.{id}.id文件 1 的键是 card_sets.{id}.id
File 2 key would be cards.{id}.card_set_id文件 2 的密钥是 card.{id}.card_set_id
I don't really care about any of the results in the count/results/meta elements, but I hope to pull together a list that sort of looks like我并不真正关心计数/结果/元元素中的任何结果,但我希望将一个看起来像的列表放在一起
File1.id, File1.title, File1.active_currencies, File2.id, File2.currency File1.id、File1.title、File1.active_currencies、File2.id、File2.currency
I've only been working with PowerShell for a day and a half and just getting the JSON files out has been a big step for me but all the google searches I can find so far are based on identical json files being merged into one.我只使用 PowerShell 一天半,刚刚取出 JSON 文件对我来说是一大步,但到目前为止我能找到的所有谷歌搜索都是基于相同的 Z466DEEC76ECDF5FCA6D38571F6324 文件This seems a little more complex and I'm stumped for the moment.这似乎有点复杂,我现在很难过。
Can anyone assist?有人可以帮忙吗?
You could try the following:您可以尝试以下方法:
$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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.