> contains the locations of the chain’s hotels in JSON. Using a regular expression, I converted the hotel data into CSV.
wat.
wat.
At the risk of providing something useful to the discussion though, I'd like to point out the excellent tool jq: http://stedolan.github.io/jq/
Here's how to use it for the conversion in the article:
jq '.places[] | [.n,.i,.p[0],.p[1],.s] | map(tostring) | join(",")' hotelMarkers.js
(after editing hotelMarkers.js into a proper json file)1. http://en.wikipedia.org/wiki/Comma-separated_values#Basic_ru...
Using the wrong tool for a really easy job can sometimes be faster than the minimal effort of getting the right tool ready.
s/^."n":"(.+)","i":"(.+)","p":\[([\d\.\-]+),([\d\.\-]+)],"s":"(\w+)","c":"(.+)".$/\1,\2,\3,\4,\5/;
{"n":"Homewood","i":"inns_suits","p":[33.455237,-86.81964],"s":"AL","c":"1"},
and then making a regular expression that matches that line literally [1]: m/^{"n":"Homewood","i":"inns_suits","p":\[33.455237,-86.81964\],"s":"AL","c":"1"},/
_ _ _
Then replace the parts that will vary with regular expressions to capture them. We want to capture the "n" field: m/^{"n":"(.*?)","i":"inns_suits","p":\[33.455237,-86.81964\],"s":"AL","c":"1"},/
_____
and the "i" field: m/^{"n":"(.*?)","i":"(.*?)","p":\[33.455237,-86.81964\],"s":"AL","c":"1"},/
_____
and the longitude and latitudes from the "p" field: m/^{"n":"(.*?)","i":"(.*?)","p":\[(.*?),(.*?)\],"s":"AL","c":"1"},/
_____ _____
and the "s" field: m/^{"n":"(.*?)","i":"(.*?)","p":\[(.*?),(.*?)\],"s":"(.*?)","c":"1"},/
_____
We don't care about the "c" field, so I'm going to drop it: m/^{"n":"(.*?)","i":"(.*?)","p":\[(.*?),(.*?)\],"s":"(.*?)"/
If we want to be fancy, we can make sure that the latitude and longitude consist only of digits, decimal points, and minus signs: m/^{"n":"(.*?)","i":"(.*?)","p":\[([\d.-]*?),([\d.-]*?)\],"s":"(.*?)"/
____ ____
For a one time thing like this, I'd probably deal with this data with a pipe in the shell, rather than use regular expressions: tr : , < in | tr -d '[]' | cut -d , -f 2,4,6,7,9 > out.csv
[1] I shall use Perl regular expressionSo, it doesn't matter much, but it was an odd choice.
... for you.
insert into my_table values ($$ json_goes_here $$);
Works for all stringy things.