Reputation: 57
I need to parse specific fields from a Shopify Webhook that has been exported in JSON into CSV file. One of the fields I need to target is the shipping address, how do I select "shipping_address" ==> "address1":"123 Shipping Street"?
{
"created_at":"2014-12-30T11:58:01-05:00",
"email":"[email protected]",
"name":"#9999",
"subtotal_price":"229.94",
"taxes_included":false,
"order_number":1234,
"billing_address":{
"address1":"123 Billing Street",
"address2":null,
"city":"Billtown",
"company":"My Company",
"country":"United States",
"first_name":"Bob",
"last_name":"Biller",
"latitude":null,
"longitude":null,
"phone":"555-555-BILL",
"province":"Kentucky",
"zip":"K2P0B0",
"name":"Bob Biller",
"country_code":"US",
"province_code":"KY"
},
"shipping_address":{
"address1":"123 Shipping Street",<!-- NEED THIS ADDRESS -->
"address2":null,
"city":"Shippington",
"company":"Shipping Company",
"country":"United States",
"first_name":"Steve",
"last_name":"Shipper",
"latitude":null,
"longitude":null,
"phone":"555-555-SHIP",
"province":"Kentucky",
"zip":"K2P0S0",
"name":"Steve Shipper",
"country_code":"US",
"province_code":"KY"
},
"customer":{
"accepts_marketing":false,
"created_at":null,
"email":"[email protected]",
"first_name":"John",
"id":null,
"last_name":"Smith",
"last_order_id":null,
"multipass_identifier":null,
"note":null,
"orders_count":0,
"state":"disabled",
"total_spent":"0.00",
"updated_at":null,
"verified_email":true,
"tags":"",
"last_order_name":null,
"default_address":{
"address1":"123 Elm St.",
"address2":null,
"city":"Ottawa",
"company":null,
"country":"Canada",
"first_name":null,
"id":null,
"last_name":null,
"phone":"123-123-1234",
"province":"Ontario",
"zip":"K2H7A8",
"name":"",
"province_code":"ON",
"country_code":"CA",
"country_name":"Canada",
"default":true
}
}
Upvotes: 1
Views: 1196
Reputation: 2058
this is a way to get data json from a webhook:
class Data{
private $arrayOrder = array();
public function __construct(){
$this->process();
}
private function process(){
$order = $this->getContents();
if(!$order){
error_log('something failed');
exit();
}
$this->arrayOrder = array(
'email'=>$order->email,
'billing_address_address1'=>$order->billing_address->address1,
'shipping_address_address1'=>$order->shipping_address->address1,
'customer'=>array(
'first_name'=>$order->customer->first_name,
'default_address'=>$order->customer->default_address->address1
)
);
error_log(json_encode($this->arrayOrder));//to test
}
private function getContents(){
$webhookContent = "";
$webhookContent = file_get_contents("php://input");
if($webhookContent){
$data = json_decode($webhookContent);
return $data;
}
return false;
}
}
$csv = new Data();
to create a csv, use the manual example to put csv :http://php.net/manual/en/function.fputcsv.php
Upvotes: 1
Reputation: 11427
Use a JSON parser of course. The most obvious answer is always the one I go with. Since JSON can be directly interpreted with Javascript, your scripting language of choice could be Javascript to do this! If you cannot for some reason write your scripting code in Javascript, almost all other scripting languages of note come with a JSON parse instruction. In Ruby for example, you simply do a
data = JSON.parse someJSON
and now you can play with data. By not telling the world your choice of scripting language in your question, or whether you are in client or server world, it is nearly impossible to help you out more. Lucky for you, JSON is just text as is your CSV file. So nothing is stopping you from parsing your JSON using a JSON parser or just plain old Javascript.
Upvotes: 0