• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Extracting value from JSON object in PHP

xoggoth

More fingers than teeth
Joined
Jul 8, 2005
Messages
17,133
Likes received
4,950
Thanks received
1,184
Location
xoggoth towers
Getting latest customer data from Stripe checkout in PHP:

$customers = \Stripe\Customer::all(array("limit" => 1, "created" => array("gte" => strtotime("-5 hour"))));
foreach ($customers->data as $customer)
{
$email = $customer->email;
$address = $customer->shipping;
etc. etc.

$email is fine but echo of $address gives:

Stripe\StripeObject JSON: { "address": { "city": "Earwig Land", "country": "GB", "line1": "Dump, Some Road", "line2": "Nasty Village", "postal_code": "DD207DX", "state": "" }, "name": "Frankenstein", "phone": null };

Not sure how I extract value of, say, postal code from that as rather confused about using JSON objects. Any ideas? Ta.

PS Edited out me own address and name obviously!
 
Is this serverside or client side?

Assuming clientside, using Javascript you would convert the JSON (which is a string) into an object.

let myObject = JSON.parse(myJSONString);

Then you can access the keys like any normal object

myObject.value
 
After having re-read your post I see you're echoing the string so it's PHP land.

json_decode(yourJSONString)

will return you an associative array ...
 
Much ta suity. Tried this

$details = json_decode($address,true);
$postcode = $details["postal_code"];
echo "XXXXXXXXX$postcodeYYYYYYYYYY";

Just getting XXXXXXXXX. Ditto with:

$details = json_decode($address);
$postcode = $details->postal_code;

Not sure what I'm cocking up. Oh well, try again tomorrow. Cheers!
 
Last edited: 20:19
PHP:
print_r($details);

or

PHP:
echo vardump($details);

Then you can see what's going on under the bonnet.

or in one line

PHP:
var_dump(json_decode($json, true));

chuck in

PHP:
echo 'Last error: ', json_last_error_msg(), PHP_EOL, PHP_EOL;

Best of luck :smile
 
Also from what I see Address is a stripe object which has a toJSON() method you need to call first I think

PHP:
$foo = json_decode($customer->address->toJSON());
 
Cheers Suity, your last post bang on, I did need to convert address object to json before converting to array. This actually shows the city:

$customers = \Stripe\Customer::all(array("limit" => 1, "created" => array("gte" => strtotime("-2 day"))));
$customer = $customers->data[0];
$custaddress = $customer->address->toJSON();
$address = json_decode($custaddress,true);
echo "<h3>$address[city]</h3>";
 
Last edited: 12:27
Cheers Suity, your last post bang on, I did need to convert address object to json before converting to array. This actually shows the city:

$customers = \Stripe\Customer::all(array("limit" => 1, "created" => array("gte" => strtotime("-2 day"))));
$customer = $customers->data[0];
$custaddress = $customer->address->toJSON();
$address = json_decode($custaddress,true);
echo " [h=3]$address[city][/h] ";

:banana:
 

Advertisers

Back
Top