javascript - shopify JSON response to CSV with node.js -
i know whats best way read json response shopify? i'm using node.js installed in windows machine , have created in site private app helps me connect admin panel through it.
this code called export.js:
var request = require('request'); var fs = require('fs'); var json2csv = require('json2csv'); var async = require('async'); var _ = require('underscore'); var ratelimiter = require('limiter').ratelimiter; var flatten = require('flat'); var limiter = new ratelimiter(1, 500); var apikey = [**********************']; var password = [***********************']; var shopname = ['itofficeberat']; var baseurl = 'https://'+apikey+':'+password+'@'+shopname+'.myshopify.com';; var numorders = 0; var orders = []; var getorders = function(page, callback) { request({ url: baseurl+'/admin/orders.json'+page, json: true }, function (error, response, body) { if (!error && response.statuscode === 200) { var newlist = []; (var = 0; < body.orders.length; i++) { newlist.push(flatten(body.orders[i])); } orders = orders.concat(newlist); console.log('received page :'+page+' - count: '+newlist.length); console.log('orders len:'+orders.length); console.log(); } callback(); }) } request({ url: baseurl+'/admin/orders/count.json?status=any', json: true }, function (error, response, body) { if (!error && response.statuscode === 200) { numorders = body.count; } console.log('total order count :'+numorders); console.log(); var numpages = numorders / 250; var r = _.range(1, numpages+1); async.foreach(r, function(page, callback) { limiter.removetokens(1, function() { getorders(page, callback); }) }, function(err) { // called when finished console.log('total orders: '+orders.length) json2csv( {data: orders, fields : ['buyer_accepts_marketing','cancel_reason']}, function(err, csv) { if (err) console.log(err); fs.writefile('shopifyorders.csv', csv, function(err) { if (err) throw err; console.log('file saved'); }); }); }); });
// code above connects site, call url: baseurl+'/admin/orders.json'+page, // file shopifyorders.csv created fields fields : ['buyer_accepts_marketing','cancel_reason'] // there no other information included in csv file. empty, should have other information json response. // // shopify json response is:
{"orders":[{"buyer_accepts_marketing":false,"cancel_reason":null,"cancelled_at":null,"cart_token":"77a4969f441e36e013da6b2fb8131bf5","checkout_token":"dd5013f1596f3413a5877daaffaab8af","closed_at":null,"confirmed":true,"created_at":"2015-04-23t06:50:03-04:00","currency":"all","device_id":null,"email":"arensmyzyri@gmail.com","financial_status":"paid","id":407850657,"landing_site":"\/","location_id":null,"name":"#1001","note":null,"number":1,"processed_at":"2015-04-23t06:50:03-04:00","reference":null,"referring_site":"","source_identifier":null,"source_url":null,"subtotal_price":"2000.00","taxes_included":false,"test":true,"token":"6772e391aedcdf798a0e9f4c5cf6e272","total_discounts":"0.00","total_line_items_price":"2000.00","total_price":"2410.00","total_price_usd":"18.52","total_tax":"400.00","total_weight":0,"updated_at":"2015-04-23t06:56:11-04:00","user_id":null,"browser_ip":"79.106.109.210","landing_site_ref":null,"order_number":1001,"discount_codes":[],"note_attributes":[],"processing_method":"direct","source":"checkout_next","checkout_id":485372325,"source_name":"web","fulfillment_status":null,"tax_lines":[{"price":"400.00","rate":0.2,"title":"vat"}],"tags":"","line_items":[{"fulfillment_service":"manual","fulfillment_status":null,"gift_card":false,"grams":0,"id":711008221,"price":"2000.00","product_id":465783109,"quantity":1,"requires_shipping":true,"sku":"","taxable":true,"title":"my product","variant_id":1221252657,"variant_title":"m \/ white \/ silver","vendor":"itofficeberat","name":"my product - m \/ white \/ silver","variant_inventory_management":null,"properties":[],"product_exists":true,"fulfillable_quantity":1,"total_discount":"0.00","tax_lines":[{"price":"400.00","rate":0.2,"title":"vat"}]}],"shipping_lines":[{"code":"standard shipping","price":"10.00","source":"shopify","title":"standard shipping","tax_lines":[{"price":"0.00","rate":0.2,"title":"vat"}]}],"billing_address":{"address1":"22 tetori","address2":"","city":"berat","company":"dfgfd","country":"albania","first_name":"arens","last_name":"myzyri","latitude":40.712601,"longitude":19.93848,"phone":"00355","province":"","zip":"5001","name":"arens myzyri","country_code":"al","province_code":null},"shipping_address":{"address1":"22 tetori","address2":"","city":"berat","company":"dfgfd","country":"albania","first_name":"arens","last_name":"myzyri","latitude":40.712601,"longitude":19.93848,"phone":"00355","province":"","zip":"5001","name":"arens myzyri","country_code":"al","province_code":null},"fulfillments":[],"client_details":{"accept_language":"en-us,en;q=0.8","browser_height":979,"browser_ip":"79.106.109.210","browser_width":1903,"session_hash":"0ade3a3a8f0729310d4de57b9c736e8efa0e44f4604b191c5c0e1d6d333d3f4f","user_agent":"mozilla\/5.0 (windows nt 6.1; wow64) applewebkit\/537.36 (khtml, gecko) chrome\/42.0.2311.90 safari\/537.36"},"refunds":[],"customer":{"accepts_marketing":false,"created_at":"2015-04-23t06:48:36-04:00","email":"arensmyzyri@gmail.com","first_name":"arens","id":383764389,"last_name":"myzyri","last_order_id":null,"multipass_identifier":null,"note":null,"orders_count":0,"state":"disabled","total_spent":"0.00","updated_at":"2015-04-23t06:56:11-04:00","verified_email":true,"tags":"","last_order_name":null,"default_address":{"address1":"22 tetori","address2":"","city":"berat","company":"dfgfd","country":"albania","first_name":"arens","id":458239865,"last_name":"myzyri","phone":"00355","province":"","zip":"5001","name":"arens myzyri","province_code":null,"country_code":"al","country_name":"albania","default":true}}}]}
Comments
Post a Comment