59 comments

[ 2.3 ms ] story [ 114 ms ] thread
> contains the locations of the chain’s hotels in JSON. Using a regular expression, I converted the hotel data into CSV.

wat.

I would assume because PostgreSQL (the DB he is using) doesn't support importing from JSON?
But why not use a JSON parser?
The dude had a whimsical idea, tried it out, and wrote it up for our enjoyment. Let it go. Maybe he will parse the JSON next time.
(comment deleted)
Next you're going to say you didn't build Pinboard with Angular, or something crazy like that.
if you're driving a nail into soft wood, you could use a hammer... Or you could just use a rock, or another piece of wood, or....

Using the wrong tool for a really easy job can sometimes be faster than the minimal effort of getting the right tool ready.

I guess, but honestly using a JSON parser sounds like less work. I mean, using regexes for this quick job is fine, it just sounded weird.
On the one hand I agree with the "It doesn't matter here" crowd, but on the other hand, Python (only language mentioned in the article) has a json library in the stdlib, and it's certainly easier than doing it with regexes.

So, it doesn't matter much, but it was an odd choice.

Even if it's in the standard library, it still might be more work to find where it is in the standard library than just do it the way you already know how.
Agreed... It's a devil you know vs. the "oh I'll just use this other library to do $simple_thing, how hard can it really be?" Hours later you realize that you didn't think it completely through and it's a bit tougher than you thought.
>it's certainly easier than doing it with regexes.

... for you.

The regexp must have taken some time to write and debug:

s/^."n":"(.+)","i":"(.+)","p":\[([\d\.\-]+),([\d\.\-]+)],"s":"(\w+)","c":"(.+)".$/\1,\2,\3,\4,\5/;

He could also have had that sitting around from a recent project.. it doesn't really seem worth second-guessing.
It's actually pretty easy and quick to write when the data is highly regular (no pun intended!). You can write this kind of expression by taking one of the lines of input:

   {"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 expression
I've been a Perl programmer and regular expression user for over a decade. It didn't take long and if you use them regularly, it's second nature. I agree, I could have used a JSON parser, but the source was well-structured and sed seemed easier. PostgreSQL can import JSON, but I haven't had a good experience with it so far.
You put together a really awesome project! Kudos on that and for sharing it first and foremost!! I speak regex pretty fluently, and I may very well have reached for sed/perl -ne there as well, depending on mood. I guess it just struck me as odd to use a parser for XML but not JSON. Anyhow, off on a tangent, I recently learned about a program called xgrep that lets you pull elements out of XML using xpath or pcre (althought pcre support was kind of spotty for me)--it's neat for one-offs like this and pipeline building while playing with rest apis and such!
Maybe, if you're doing it all at once. I often use my text editor as a regex platform, using the Find/Replace menu to dice off chunks of text in a series of quick and easy operations, using UNDO if I get one wrong. It's really very quick if you don't force yourself to do it in one step.
It depends on the version. More recent releases (that is, >= 9.2) do have native JSON support, but it's still young — though steadily improving.
Yup, I've done a bit with 9.2, and it accepts JSON just fine (although not with automatic string conversion) but it's a short road to wishing the data were in standard tables and columns.
Use dollar-quoting for automatic string conversion:

insert into my_table values ($$ json_goes_here $$);

Works for all stringy things.

Cool, thanks. Last time around I used a pg-driver-specific datatype to hold the parameter on its way through sql2o, and felt rather guilty about doing so.
It was a bit odd to me, but I was willing to accept that it might be faster than figuring out the API for spitting out csv, but when he did exactly that for the XML extraction...
You kids and your fancy API's. I remember back in the day when we had to... something something regexes.
(comment deleted)
I agree with others that it doesn't matter in this case. The author used a tool he knows to perform a simple job.

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)
And here is the reason I will never post code I write.
I feel the same way. Heaven forbid that I do something that I know how to do that gets the job done and it's not the perfect way or is even, perhaps, completely silly. Let's focus on that thing and not the overall project.
Why? If other people do something differently it's a great way to learn something new. Don't take it personally.
I think its more that the "wat." comment wasn't a chance to learn anything new. It was just snark.
In the closed source GIS world JSON hasn't exactly taken the world by storm. In open source (and I'm assuming the author is pretty familiar, seeing as he's using Postgres) GeoJSON is the format of choice. Wrangling that data INTO GeoJSON would net little advantage all for the purpose of importing it into Postgres. For displaying the results in a wonderful tool like QGIS, I could see going the extra mile.
Who cares? It worked, didn't it? Maybe he knows regexes better than JSON parsers.
It's deliberate:

"Between February and June of 1969 ... no more full-service properties were planned ... difficult to control quality with in-house restaurants ... All inns built after La Quinta #505 were built ... at locations with area available to build a restaurant ... which would be leased to a major restaurant chain for management."

"June, 1969 ... La Quinta #507 ... Restaurant on the premises was leased to Denny's."

http://www.business.txstate.edu/users/jb15/MGT4350/how_la_qu...

I noticed the same thing with Noah's Bagels and Jamba Juice. Apparently there is also a connection between the two: http://breakfastatepiphany.blogspot.com/2012/06/noahs-new-yo...
A rap duo from Brooklyn noticed the same thing with Pizza Hut and Taco Bell... http://www.youtube.com/watch?v=EQ8ViYIeH04
The reason for this would be that both, Pizza Hut & Taco Bell (as well as KFC) are owned by the same company: Yum Brands.
Correct. It is even possible to find restraints that are dual or triple restaurants, serving food for all three.
Which is what the song is about.
(comment deleted)
Actually, they are talking about combination pizza hut and taco bells, which are in the same building (and owned by the same parent company). So it is slightly different. They are literally the exact same location, not just close by.
A rather peculiar one my friend noticed: Apple stores and Victoria's Secret.
They stick both in 'high end' shopping areas.
Reminds me of this classic article on a business strategy of opening a coffee shop across the road from a Starbucks - http://www.slate.com/articles/news_and_politics/hey_wait_a_m...
I always thought it was funny that for a while Starbucks operated two shops on opposite corners of the same intersection at Robson & Thurlow in Vancouver.

https://www.google.com/maps/@49.284602,-123.12482,3a,75y,229...

One of them has been closed since this Street View image was taken in 2012, but they were both there for some time (I had worked in the area in 2008 and they both were definitely there then).

I would think that's somewhat similar to two gas stations on opposite corners. You can get in and out of one of them fairly easily from any direction and going back out in your desired direction.

You're not invested (usually) in going to a particular Starbucks when you're getting your coffee on the go. This lets them increase the throughput for the area, while also capturing traffic (foot traffic in the case of this setup it seems) from more directions. You want coffee, but it's on the opposite corner then you have to cross two roads two times each? That's inconvenient. You have to cross, in the worst case, one road twice to get coffee in this setup. (Note: I don't think people analyze their behavior to that depth, but ease of access is at least an unconcious factor in determining whether to visit a place.)

Clustering is also a benefit to supply chain logistics, although it would be a bit extreme to do it just for that.
(comment deleted)
"So, only 3.4% of the La Quintas out there live up to Mitch Hedberg’s expectations...Update:...This yields 49 pairs (or 5.8% of all La Quintas)"

Brings to mind:

"Humor can be dissected, as a frog can, but the thing dies in the process and the innards are discouraging to any but the pure scientific mind" - E.B. White

Almost no comments about the amazing comedy of Mitch? I hadn't known about him until today ... The kinds of observation he makes are hilarious. Any fans?
Mitch Hedberg is amazing

"Dogs are forever in the pushup position" ~ Mitch

One thing missed here is this sort of thing is a lot easier (to read and code) using the built-in earthdistance module.

The query doesn't need the added "shape" column unless you want it for indexes, and becomes simply:

    SELECT d.city, d.state, earth_distance(
                    ll_to_earth(d.latitude, d.longitude),
                    ll_to_earth(l.latitude, l.longitude)) as distance
    FROM dennys d, laquinta l 
    WHERE distance <= 150
    ORDER BY 3
No more magic numbers or confusing function names.

Note I don't mean this as a slight on the article - I purely mean it to educate postgres users that they can do this sort of thing easily without downloading/installing PostGIS.

It looks like this comedian died in 2005. So I am wondering what the percentage was when he was alive and making this observation.