• 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!

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.

Previously on "Extracting value from JSON object in PHP"

Collapse

  • suityou01
    replied
    Originally posted by xoggoth View Post
    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 " $address[city]

    ";

    Leave a comment:


  • xoggoth
    replied
    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:


  • suityou01
    replied
    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:


  • suityou01
    replied
    PHP Code:
    print_r($details); 
    or

    PHP Code:
    echo vardump($details); 
    Then you can see what's going on under the bonnet.

    or in one line

    PHP Code:
    var_dump(json_decode($jsontrue)); 
    chuck in

    PHP Code:
    echo 'Last error: 'json_last_error_msg(), PHP_EOLPHP_EOL
    Best of luck

    Leave a comment:


  • xoggoth
    replied
    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:


  • suityou01
    replied
    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:


  • suityou01
    replied
    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:


  • xoggoth
    started a topic Extracting value from JSON object in PHP

    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!

Working...
X