Originally posted by xoggoth
View Post
- Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
- 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!
Reply to: Extracting value from JSON object in PHP
Collapse
You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:
- You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
- You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
- If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.
Logging in...
Previously on "Extracting value from JSON object in PHP"
Collapse
-
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 by xoggoth; 12 November 2021, 13:38.
Leave a comment:
-
Also from what I see Address is a stripe object which has a toJSON() method you need to call first I think
PHP Code:$foo = json_decode($customer->address->toJSON());
Leave a comment:
-
PHP Code:print_r($details);
PHP Code:echo vardump($details);
or in one line
PHP Code:var_dump(json_decode($json, true));
PHP Code:echo 'Last error: ', json_last_error_msg(), PHP_EOL, PHP_EOL;
Leave a comment:
-
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 by xoggoth; 11 November 2021, 20:30.
Leave a comment:
-
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 ...
Leave a comment:
-
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
Leave a comment:
-
Extracting value from JSON object in PHP
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!Tags: None
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers
Contractor Services
CUK News
- How to answer at interview, ‘What’s your greatest weakness?’ Nov 14 09:59
- Business Asset Disposal Relief changes in April 2025: Q&A Nov 13 09:37
- How debt transfer rules will hit umbrella companies in 2026 Nov 12 09:28
- IT contractor demand floundering despite Autumn Budget 2024 Nov 11 09:30
- An IR35 bill of £19m for National Resources Wales may be just the tip of its iceberg Nov 7 09:20
- Micro-entity accounts: Overview, and how to file with HMRC Nov 6 09:27
- Will HMRC’s 9% interest rate bully you into submission? Nov 5 09:10
- Business Account with ANNA Money Nov 1 15:51
- Autumn Budget 2024: Reeves raids contractor take-home pay Oct 31 14:11
- How Autumn Budget 2024 affects homes, property and mortgages Oct 31 09:23
Leave a comment: