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

Extracting value from JSON object in PHP

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

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

    If everything isn't black and white, I say, 'Why the hell not?'
    John Wayne (My guru, not to be confused with my beloved prophet Jeremy Clarkson)

    #2
    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
    Knock first as I might be balancing my chakras.

    Comment


      #3
      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 ...
      Knock first as I might be balancing my chakras.

      Comment


        #4
        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.
        bloggoth

        If everything isn't black and white, I say, 'Why the hell not?'
        John Wayne (My guru, not to be confused with my beloved prophet Jeremy Clarkson)

        Comment


          #5
          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
          Knock first as I might be balancing my chakras.

          Comment


            #6
            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()); 
            Knock first as I might be balancing my chakras.

            Comment


              #7
              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.
              bloggoth

              If everything isn't black and white, I say, 'Why the hell not?'
              John Wayne (My guru, not to be confused with my beloved prophet Jeremy Clarkson)

              Comment


                #8
                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]

                ";
                Knock first as I might be balancing my chakras.

                Comment

                Working...
                X