4 ms·
But why not use a JSON parser?
by wtetzner 12y ago
But why not use a JSON parser?
- idlewords 12y agoThe 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.
- deleted 12y ago[deleted]
- aaronbrethorst 12y agoNext you're going to say you didn't build Pinboard with Angular, or something crazy like that.
- knodi123 12y agoif 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.
- wtetzner 12y agoI 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.
- jlarocco 12y agoOn 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.
- recursive 12y agoEven 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.
- weaksauce 12y agoAgreed... 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.
- aestra 12y ago>it's certainly easier than doing it with regexes. ... for you.
- coolj 12y agoThe regexp must have taken some time to write and debug: s/^."n":"(.+)","i":"(.+)","p":\[([\d\.\-]+),([\d\.\-]+)],"s":"(\w+)","c":"(.+)".$/\1,\2,\3,\4,\5/;
- fenomas 12y agoHe could also have had that sitting around from a recent project.. it doesn't really seem worth second-guessing.
- tzs 12y agoIt'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
- johnjreiser 12y agoI'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.