4 ms·
PowerShell, the right tool for the job: (iwr https://gist.githubusercontent.com/jorin-vogel/7f19ce95a9a842956358/raw/e319340c2f6691f9cc8d8cc57ed532b5093e36
by useerup 11y ago
PowerShell, the right tool for the job:
(iwr https://gist.githubusercontent.com/jorin-vogel/7f19ce95a9a842956358/raw/e319340c2f6691f9cc8d8cc57ed532b5093e3619/data.json | ConvertFrom-Json) | ? creditcard | select name,creditcard | Export-Csv ('{0:yyyyMMdd}.csv' -f (Get-Date)) -not
Edit: the single command written out to multiple (continued) lines in the PowerShell "long" form - i.e. no aliases and parameter names explicitly named.
(Invoke-WebRequest -Uri https://gist.githubusercontent.com/jorin-vogel/7f19ce95a9a842956358/raw/e319340c2f6691f9cc8d8cc57ed532b5093e3619/data.json | ConvertFrom-Json) |
Where-Object creditcard |
Select-Object -Property name,creditcard |
Export-Csv -Path ('{0:yyyyMMdd}.csv' -f (Get-Date)) -NoTypeInformation
- orf 11y agoLooks a lot more readable than the UNIXy alternatives below
- useerup 11y agoyeah. And unlike many of the Unixy solutions it will actually produce correct results even if the input data contained quotes ("), apostrophes ('), commas (,), backslashes (\) and other characters that could throw parsing and text synthesizing off. * The iwr (Invoke-WebRequest) reads the text file from the uri. * The ConvertFrom-Json cmdlet reads the content as json and parses it according to json rules. The output is a sequence of objects (not json objects, but PowerShell objects) with properties corresponding to the json fields. This will respect specially encoded characters. * The ? (select-object) cmdlet can filter on complex expressions, but in this case it just filters on the presence of a creditcard property. * The select (Select-Object) cmdlet selects just the properties we want to export, out of all of the properties for each object. The epcsv (Export-Csv) exports objects from the pipeline and outputs them to a file with proper escaping for e.g. quotes, apostrophes etc. By not relying on home-cooked text munging you can actually produce a robust solution that is also readable.
- useerup 11y agoReplying to self because I learned something new about PowerShell today :) On the challenge github page dfinke used irm (Invoke-RestMethod) instead of iwr (Invoke-WebRequest). It turns out that irm actually looks at the content type of the response and converts from json to objects implicitly if the content type indicates json. Thanks to dfinke. The above PowerShell one-liner can thus be written shorter (omitting the explicit ConvertFrom-Json invokation): (irm https://gist.githubusercontent.com/jorin-vogel/7f19ce95a9a842956358/raw/e319340c2f6691f9cc8d8cc57ed532b5093e3619/data.json) | ? creditcard | select name,creditcard | epcsv ('{0:yyyyMMdd}.csv' -f (Get-Date)) -not
- jclos 11y agoI always dismissed PowerShell but your comment intrigues me. What would you recommend as a starting resource for learning it?
- useerup 11y agohttps://technet.microsoft.com/en-us/library/cc281945(v=sql.105).aspx https://technet.microsoft.com/en-us/library/cc281945(v=sql.1... If you are more of a developer you may want to start at MSDN instead: https://msdn.microsoft.com/en-us/library/dd835506(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/dd835506(v=vs.85).a... I recently found out about Microsoft Virtual Academy. There are some seriously good (free) online courses there, featuring Jeffrey Snover himself (author of the Monad Manifest and inventor of PowerShell). For example this one: http://www.microsoftvirtualacademy.com/training-courses/getting-started-with-powershell-3-0-jump-start http://www.microsoftvirtualacademy.com/training-courses/gett... Downside is that you do not control the pace yourself as much as by written resources. There's a very friendly community at http://powershell.org http://powershell.org where you can ask questions, get advice etc.