Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 657

How to put data from CSV to JSON?

$
0
0

I have a CSV containing two "columns" 'User id' and 'email' example:

User id,email1234-1234-1234,some@email.address321-1235-44432,anoteher@email.address322136231345,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-Jsonforeach ($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.


Viewing all articles
Browse latest Browse all 657

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>