Pages

Wednesday, April 22, 2009

YAML to the Rescue

I needed to create and import product data into an SQL database, but the information I had been given was scattered through various email messages from the client. There was no structure, no CSV file, and no time -- as usual.

It was shaping up to be a real bother to create a CSV file from the information I had and CSV wasn't exactly the friendliest format to look at with poor human eyes.

So I created a YAML file with an entry representing each product. Since the project was mostly written in PHP, I used spyc to parse the file and convert it to SQL insert statements. YAML was much easier to look at than CSV and allowed me to quickly organize the product data without mistakes (such as putting a piece of data in the wrong column).

Writing the parse was easy because each product was already generated as an object, so one could say something like:

foreach ($Yaml as $Product) { print "INSERT INTO product VALUES ('$Product->title', ...); }

I've used YAML for meta data and other applications, but this quick and dirty use saved a lot of time and frustration.

No comments:

Post a Comment