Well you could get Magento to do all the work, I suppose. Why would you want to do that, you're just taking resource off your store? As to moving text files around via JSON - you've got to extract it, dump it somewhere, arrange security on the file location, consume the JSON string across the network, parse it into a database location for reporting and keep an eye on the job to make sure it's running reliably.
Or you could just use ODBC and talk directly to MySQL.
Actually in terms of the question, it's tricky because you have to understand all of MagnetosMagentos little quirks, from your code I can see immediately that you're not accounting for configurable and simple items, so the danger here is that you're duplicating lines on the join, you need to ensure you're getting uniques before joining to sales. I'd build a product model query, build a stock model query and build a sales model and then relate them together on the other side of the fence, it makes life a lot easier. You could create a MySQL stored procedure to do this, but they're not very efficient because MySQL doesn't precompile.