0

I'm new to PHP and JSON, so hoping someone can help me.

I have a PHP which a 3rd party performs a POST against, delivering JSON data. An example of the data sent would be:

> {   "created": 1326853478,   "livemode": false,   "id":
> "evt_00000000000000",   "type": "charge.succeeded",   "object":
> "event",   "data": {
>     "object": {
>       "id": "ch_00000000000000",
>       "object": "charge",
>       "created": 1366838716,
>       "livemode": false,
>       "paid": true,
>       "amount": 1000,
>       "currency": "gbp",
>       "refunded": false,
>       "fee": 59,
>       "fee_details": [
>         {
>           "amount": 59,
>           "currency": "usd",
>           "type": "stripe_fee",
>           "description": "Stripe processing fees",
>           "application": null,
>           "amount_refunded": 0
>         }
>       ],
>       "card": {
>         "object": "card",
>         "last4": "4242",
>         "type": "Visa",
>         "exp_month": 2,
>         "exp_year": 2016,
>         "fingerprint": "cniJDyeew54ashW6Iyr",
>         "country": "US",
>         "name": "wibble3",
>         "address_line1": null,
>         "address_line2": null,
>         "address_city": null,
>         "address_state": null,
>         "address_zip": null,
>         "address_country": null,
>         "cvc_check": "pass",
>         "address_line1_check": null,
>         "address_zip_check": null
>       },
>       "captured": true,
>       "failure_message": null,
>       "amount_refunded": 0,
>       "customer": null,
>       "invoice": null,
>       "description": null,
>       "dispute": null
>     }   } }

I would like to be able to extract certain items to then process depending on these values.

Using this code, I can extract the 'type' easily enough:

$body = @file_get_contents('php://input');
$event_json = json_decode($body);
print $event_json->{'type'};

But I cannot extract "fee" for example, which seems to be a level below where I am successfully extracting a value from, or "description" which is another level down again.

I would like to be able to extract only certain items from this JSON string (for example, Type, Fee, Description, Last4), but I am completely lost. I appreciate this is likely a fairly basic task, but I'm getting no where.

Any help, gratefully received!

1
  • Have you tried calling json_last_error() after your call to json_decode to make sure everything is being read properly? Commented Apr 25, 2013 at 15:26

4 Answers 4

2

First, I would suggest passing true as the second argument to json_decode. This results in an associative array rather than an object, which makes it a little easier to work with.

Then, you can do this:

$fee = $event_json['data']['object']['fee'];

You can chain these square brackets as many times as needed to delve deeper into the array.

Sign up to request clarification or add additional context in comments.

Comments

2

You can quickly acheive this in following manner:

$body = @file_get_contents('php://input');
$event_json = json_decode($body, true);
print $event_json['data']['object']['fee'];

Comments

1

you have multi levels of objects so use:

echo($event_json->data->object->fee);

Basically you are echoing fee, which is a member of the object name object, which in turn is the attribute of an object named data, which again in turn is a member of your $event_json object.

Comments

0

When you do a json_decode, you get an array of this data. So, you can access data like this:

$event_json['data']['object']['fee']

good luck

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.