I have a CSV containing two "columns" 'User id'
and 'email'
example:
User id,email
1234-1234-1234,some@email.address
321-1235-44432,anoteher@email.address
322136231345,more.email@address.too
And a JSON looking like this:
[{
"externalId": "100000",
"watchers": ["some@email.address", "anoteher@email.address", "more.email@address.too"]
},
{
"externalId": "100002",
"watchers": ["anoteher@email.address", "more.email@address.too"]
}
]
What I'm trying to do is to replace the email addresses in the JSON with the 'User id' from the CSV accordingly. So far I have the inefficient foreach in foreach code but it only replaces the first email in the watchers array.
$usersCSV = Import-Csv 'users.csv'
$watchersJSON = Get-Content -Path "watchers.json" -raw |ConvertFrom-Json
foreach ($watchersJSONdata in $watchersJSON) {
foreach ($usersCSVdata in $usersCSV){
if ($watchersJSONdata.watchers -eq $usersCSVdata.email) {
$watchersJSONdata.watchers = $usersCSVdata.'User id'
}
}
} $watchersJSON |ConvertTo-Json | out-file "watchers-with-ID.json"
Result is:
[{
"externalId": "100000",
"watchers": ["1234-1234-1234"]
}
]
I'm still working on it but a little help would be great.
I wouldn't mind a completely different approach using a single line jq but I don't know jq at all.
Easiest and more efficient way to do it is with Group-Object -AsHashtable
to leverage a dictionary type for fast lookups. See about_Hash_Tables for more details.
$usersCSV = Import-Csv 'users.csv' | Group-Object email -AsHashTable
$watchersJSON = Get-Content -Path 'watchers.json' -Raw | ConvertFrom-Json
foreach ($object in $watchersJSON) {
$object.watchers = $usersCSV[$object.watchers].'User id'
}
ConvertTo-Json @($watchersJSON) | Out-File 'watchers-with-ID.json'