Home > Software engineering >  Extract element from nested JSON
Extract element from nested JSON

Time:05-24

There is json response:

{
  "id": "1234567890123456789",
  "creation_date": 12345678,
  "event": "WAITING_PAYMENT",
  "version": "2.0.0",
  "data": {
    "product": {
      "id": 213344,
      "name": "Product Name",
      "has_co_production": false
    },
    "affiliates": [
      {
        "name": "Affiliate name"
      }
    ],
    "buyer": {
      "email": "[email protected]"
    },
    "producer": {
      "name": "Producer Name"
    },
    "commissions": [
      {
        "value": 0.65,
        "source": "MARKETPLACE"
      },
      {
        "value": 3.10,
        "source": "PRODUCER"
      }
    ],
    "purchase": {
      "approved_date": 1231241434453,
      "full_price": {
          "value": 134.0
        },
      "original_offer_price": {
        "currency_value": "EUR"
        "value": 100.78,
      },
      "price": {
        "value": 150.6
      },
      "order_date": "123243546",
      "status": "STARTED",
      "transaction": "HP02316330308193",
      "payment": {
        "billet_barcode": "03399.33335 33823.303087 198801027 2 876300015000",
        "billet_url": "https://billet-link.com/bHP023163303193",
      }
    },
    "subscription": {
      "status": "ACTIVE",
      "plan": {
        "name": "plan name"
      },
      "subscriber": {
        "code": "12133421"
      }
    }
  }
}

My question is how to extract data["buyer"]["email"] in PHP ?

I only need to extract the email information from the buyer table inside the data table.

CodePudding user response:

First, you need to decode the json to a PHP array (or an object), then you can access the requested information from the decoded data.

$data = json_decode('the json string most place here', true);
$email = $data['buyer']['email'];

Place your json string in the first argument of json_decode() function.

  • Related