Notepadd++ search and replace using the regular expressions
^\{"name":("[^"]+)".+"creditcard":("[^"]+")\},?$
and replace it with
\1,\2
then sort the file using the TextFX plugin and delete all the lines without credit card number at the bottom. Type the header line and done. Five to ten minutes including some manual sanity checking. Of course don't forget to look at the calendar and save with the correct file name.
They shouldn't have put each record on a single line, that makes it way too easy for text editors. Assume that this a single line or there are arbitrary newlines inserted and then use a proper parser/filter, e.g. jq in the shell as mentioned in the comments.
Replace },{ with },\r\n{ and done. Using more sophisticated tools than a text editor only makes sense if you know them well, the task is more complex or the file larger.
Sure, which is why I suggested to make the task complex enough so that the "usual" *nix text tools aren't enough, or significantly more complex to use then a real parser. I mean it's a basically a rehash of "how do I parse HTML with regexes".
Single line, no assumptions about data previously stored, no invalid CSV with quotes getting the entire line and no useless spaces in the beginning of each line:
The first way I did it was just read the file, parse the json, check for nulls, and print. Then I piped this to a file. That is too boring though.
The next way I did it was with Vim replaces. There might be sexier ways of doing this?
1) A big replace to get rid of all the junk.
%s/\%Vemail.*creditcard/creditcard/g
2) A big replace to fix nulls in the credit card
%s/\%V.*creditcard.*null.*//g
Then I could either change my code to not check for nulls or keep cleaning up this data until I have a CSV.
This Vim flow is the exact thing I do every week or two at work when building the Chrome HSTS preload list into our product. Anyone know the sexier ways to make this really really fun?
I should note to those not super familiar with vim that may try this, my vim commands are over a visual block (the json string), since this was in the same file as my code.
It's been years since I wrote perl. Any kind soul care to comment some of this code? It looks like the logic starts at the bottom, but I must admit defeat in truly understanding how it works!
Perl is water in the cupped hands of our minds. I have yet to meet someone who can keep it from slipping between their fingers without consistent effort.
* <> reads from file(s) specified as command-line arguments (data.json in this case)
* Option -0777 tells Perl to slurp the whole file
* Option -MJSON loads the JSON module
I think it's usually better to do this with a reproducible script instead of manual editing in a text editor. There are too many times where the requirements change after the fact for "simple one-off" transformations.
I was going to say the same thing. I think of everything as black boxes, so choice of language is not as important as approach. I would use PHP because it's informal - it's a hybrid between shell and C so expressiveness and speed come basically for free and there isn't much friction. Being able to quickly iterate often makes relationships become apparent at a meta level. For example a quick lookup of a parsing error might reveal that the data was generated by a standard tool, so I could drop what I'm doing and grab that instead. It's not so much about individual choices, but a way of attacking problems that compounded over time leads to a great deal of leverage.
The one thing that was not left fully specified: Should the filename in YYYYMMDD.csv be the date that the program is run, or should a file of that spec be created grouping the lines in the .json file by their date field? If it is the latter, none of the github solutions are coding this correctly.
My PostgreSQL 9.3 solution using json_populate_recordset and @cjauvin's Python solution are the only two which group by date. It wasn't much harder, but nudges it out of shell oneliner territory.
The R example posted on the GitHub thread is very nice. [1]
I use R every day, often parsing json and writing csv files. The solution posted by mrdwab is much more succinct than my go to method, especially the use of pipes. I've read about pipes in the dplyr/magrittr packages before, but this is the first time I've seen them used and thought they made complete sense.
I agree, the solution in R seems by far the most simple and intelligible. But then R is specifically designed to do this kind of thing, so that's not too surprising.
If this was a list of cat pictures, I would not consider posting to a third party cheating. But seeing as how it's credit card data, even though it is already "in the wild," it is still worth not posting to another random website.
Considering these are credit card numbers and you are trying to protect the account holders, is it really a smart idea to upload the names and numbers to some third party website?
Lowest effort solution: Post to stackexchange/github/reddit with the question "I've got a programming challenge. Can you find the shortest code to convert this JSON to CSV?" Then sit back and watch the answers roll in.
81 comments
[ 4.9 ms ] story [ 152 ms ] threadhttp://www.transformy.io/
Or Ruby.
[1] https://stedolan.github.io/jq/
(echo '"Name","Credit Card"'; jq -r '.[] | select(.creditcard) | [.name, .creditcard] | @csv' data.json) >> `date +%Y%m%d`.csv
Another less hacky way would be:
edit: fix format and add gg after null deletion.
cat data.json | grep -v 'null}' | sed -r 's/\"name\"\:\"([a-zA-Z.'\'' -]{2,50})\",.{80,200}\"creditcard\":\"([0-9-]{2,50})\"},?/\1,\2/' | tr -d '{}]['
(replace with sed -E on Mac OSX)
Assuming you are on this URL:
you can do this (might take a few seconds):echo "name,creditcard$(wget -qO- https://gist.githubusercontent.com/jorin-vogel/7f19ce95a9a84... | jq -r 'map(select(.creditcard != null) | .name + "," + .creditcard)' | sed -E -e 's/\]|\"|\[| {2,}|,$//g')" > $(date +"%Y%m%d").csv
The next way I did it was with Vim replaces. There might be sexier ways of doing this?
1) A big replace to get rid of all the junk.
2) A big replace to fix nulls in the credit card Then I could either change my code to not check for nulls or keep cleaning up this data until I have a CSV.This Vim flow is the exact thing I do every week or two at work when building the Chrome HSTS preload list into our product. Anyone know the sexier ways to make this really really fun?
I should note to those not super familiar with vim that may try this, my vim commands are over a visual block (the json string), since this was in the same file as my code.
grep for items with a 'creditcard' field
map each item to a string with its 'name' and 'creditcard' fields
for each string (and "name,creditcard\n"), print it
into 20150425.csv
https://gist.github.com/jorin-vogel/2e43ffa981a97bc17259#com...
I use R every day, often parsing json and writing csv files. The solution posted by mrdwab is much more succinct than my go to method, especially the use of pipes. I've read about pipes in the dplyr/magrittr packages before, but this is the first time I've seen them used and thought they made complete sense.
[1] https://gist.github.com/jorin-vogel/2e43ffa981a97bc17259#com...
1) Copy all of the JSON data.
2) Paste it into this link: http://konklone.io/json/ (I don't know if this is cheating)
3) Download the file.
3a) If it opens in a new page, just manually save the source as a .csv
4) Open it in excel.
5) Click the column of credit cards.
6) Click the button to sort in descending order.
7) Scroll down to the point where you run out of credit cards.
8) Delete everything after that.
Your solution is probably more sensible as a one off than a lot of the other answers.
sit back and watch the answers and downvotes roll in.