user1012181
user1012181

Reputation: 8726

How to get the JSON response in a specific format?

I'm trying to test a Module and I need to recreate a json as follows:

$billing_info['billing']->customer_id
$billing_info['billing']['source']->exp_year

I tried to recreate it as follows:

$arr = json_encode(['id'=>'card_17LHmCI49Oxjrr81SringkGq', 'exp_year'=>2018, 'exp_month' => 07, 'funding' => 'credit', 'brand' => 'Visa', 'last4' => '4242']);

$customer = json_encode(['customer_id' => 'cus_7aSheljul3mkAB', 'source' =>json_decode($arr)]);

But I'm not able to call dd($billing_info['billing']->customer_id);

The desired JSON response is like as follows:

[
  "billing" => [
    {"customer_id": 12 }
    "source" => {
      "exp_year": 2018
      "exp_month": 7
      "funding": "credit"
      "brand": "Visa"
      "last4": "4242"
    }
  ]
]

Upvotes: 0

Views: 54

Answers (1)

Gavriel
Gavriel

Reputation: 19247

$customer = new stdClass();
$customer->customer_id=1;
$customer->source=new stdClass();
$customer->source->exp_year=2018;
$billing_info = array('billing'=>$customer);

update1:

Just realized that what you want is impossible:

$billing_info['billing']->customer_id
$billing_info['billing']['source']->exp_year

In the 1st line you assume $billing_info['billing'] is an object, while in the 2nd line you assume it's an array. Please decide which one you prefer:

$billing_info['billing']->customer_id
$billing_info['billing']->source->exp_year

$billing_info['billing']['customer_id']
$billing_info['billing']['source']->exp_year

Optionally ['exp_year'] is also possible

update2:

Please include a VALID json! You can check it here: http://jsonlint.com/

Upvotes: 1

Related Questions