Jump to content

Any pattern matching gurus wanna help me parse this?

Recommended Posts

Hey, just building a workflow to upload images to ImageShack, which is going to help me enormously with quickly adding screenshots to these forum posts.
Anyway, I really don't know what I'm doing with pattern matching more complicated than simple wildcards and the like (I really should just sit down one day and get to grip with regular expressions, but anyway) so I could do with a bit of a hand with this.
Here is the returned string after a successful image upload via the API:

{"status":"1","version":8,"timestamp":1358818117,"base_url":"imageshack.us/a/","id":985332702,"rating":{"ratings":0,"avg":0},"files":{"server":"805","bucket":"2397","image":{"size":60860,"content-type":"image/png","filename":"screenshot20130122at225.png","original_filename":"Screen Shot 2013-01-22 at 2.25.12 PM.png"},"thumb":{"size":2283,"content":"image/jpeg","filename":"screenshot20130122at225.th.png"}},"resolution":{"width":162,"height":533},"exif-info":{"exifcompression":"Deflate/Inflate","exifimagesnum":"1"},"class":"r","visibility":"yes","uploader":{"ip":"","cookie":"21b3c8f8a1b525290dadd25ef12ab2ce","username":"CarlosNZ"},"links":{"image_link":"http://imageshack.us/a/img805/2397/screenshot20130122at225.png","image_html":"<a href='http://imageshack.us/photo/my-images/805/screenshot20130122at225.png/' target='_blank'><img src='http://imageshack.us/a/img805/2397/screenshot20130122at225.png' alt='Free Image Hosting at www.ImageShack.us' border='0'/></a>'","image_bb":"[URL=http://imageshack.us/photo/my-images/805/screenshot20130122at225.png/] [/URL]","image_bb2":"[url=http://imageshack.us/photo/my-images/805/screenshot20130122at225.png/][img=http://imageshack.us/a/img805/2397/screenshot20130122at225.png]  [/url]","thumb_link":"<a href='http://imageshack.us/photo/my-images/805/screenshot20130122at225.png/' target='_blank'><img src='http://img805.imageshack.us/img805/2397/screenshot20130122at225.th.png' alt='Free Image Hosting at www.ImageShack.us' border='0'/></a>'","thumb_bb":"[URL=http://imageshack.us/photo/my-images/805/screenshot20130122at225.png/] [/URL]","thumb_bb2":"[url=http://imageshack.us/photo/my-images/805/screenshot20130122at225.png/][img=http://img805.imageshack.us/img805/2397/screenshot20130122at225.th.png]  [/url]","is_link":"http://imageshack.us/photo/my-images/805/screenshot20130122at225.png/","done":"http://imageshack.us/content_round.php?page=done&l=img805/2397/screenshot20130122at225.png"}}




That's all one line! I'm after that URL that I've highlighted to be returned to the user. What kind of funny-looking awk/sed/grep expression might I use to extract that?
Many thanks in advance for any tips you might be able to give me.

Link to comment
Yup, I really need to learn a proper programming language. I think good old bash scripting has taken me about as far as it can. Any recommendations?


I know Python is good with JSON and I found it not-too-difficult to get a feel for while I was playing with Sublime Text plugins. I'm not a software guy, really, but it seemed like with aggressive Googling and a little perseverance it was easy enough to figure out. As for your regex question, if it's always going to be a png file, this clumsily but accurately matches just the string you need, according to Patterns:



But, again, not a software guy, so there's probably a more elegant way.

Link to comment
This is a json encoded string. This is really simple to parse, depending on the language you use in your workflow.

In PHP, for instance just do:

$parsed = json_decode($api_response, true);

return $parsed['links']['image_link'];

Thanks heaps for this. With some aggressive googling, I've managed to build a tiny php script that performs this function, which I've then embedded into my bash script.  :D

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...