Can you help me figure out this notation:
$myArray = jason_encode( [{ '
'email' => '[email protected]',
'firstname' => 'peter',
'last_name' => 'griffin',
'company' => 'Nantuket Brewing',
'Addresses' => [{
'addr1' => '31 Spooner Ln',
'city' => 'Quahog',
'state' => 'RI'
}]
}]
jason_encode()
converts the data structure into a flat string of most key: value pairs and such.
I get that []
means array
Why the [{ }]
notation?
It looks like I maybe confusing combined protocols. the original string looks like:
CURLOPT_POSTFIELDS => "[\n {\n \"email\": \"[email protected]\",\n \"first_name\": \"string\",\n \"last_name\": \"string\",\n \"company\": \"string\",\n \"phone\": \"string\",\n \"notes\": \"string\",\n \"tax_exempt_category\": \"string\",\n \"customer_group_id\": 0,\n \"addresses\": [\n {\n \"address1\": \"Addr 1\",\n \"address2\": \"\",\n \"address_type\": \"residential\",\n \"city\": \"San Francisco\",\n \"company\": \"History\",\n \"country_code\": \"US\",\n \"first_name\": \"Ronald\",\n \"last_name\": \"Swimmer\",\n \"phone\": \"707070707\",\n \"postal_code\": \"33333\",\n \"state_or_province\": \"California\",\n \"form_fields\": [\n {\n \"name\": \"test\",\n \"value\": \"test\"\n }\n ]\n }\n ],\n \"authentication\": {\n \"force_password_reset\": true,\n \"new_password\": \"string123\"\n },\n \"accepts_product_review_abandoned_cart_emails\": true,\n \"store_credit_amounts\": [\n {\n \"amount\": 43.15\n }\n ],\n \"origin_channel_id\": 1,\n \"channel_ids\": [\n 1\n ],\n \"form_fields\": [\n {\n \"name\": \"test\",\n \"value\": \"test\"\n }\n ]\n }\n]",
I think what I'm seeing as PHP is actually hand json encoding.
This string works but when I try to assemble the string on my own, the API fails it with
{"status":422,"title":"The request payload has to be a JSON array for the endpoint","type":"https://developer.bigcommerce.com/api-docs/getting-started/api-status-codes","errors":{}}
This is the string I'm making:
{"email":"[email protected]","first_name":"Lucy","last_name":"van Pelt","company":"Peanuts LTD","phone":"2345551234","notes":"","tax_exempt_category":"","customer_group_id":0,"addresses":{"address1":"120 Great Pumpkin Ln","address2":"","address_type":"residential","city":"St Paul","company":"Peanuts LTD","country_code":"US","first_name":"Lucy","last_name":"van Pelt","phone":"2345551234","postal_code":"55101","state_or_province":"Minnesota","form_fields":{"name":"Are your sales tax exempt? ","value":"No"}},"authentication":{"force_password_reset":false,"New_password":"PeanutsLTD"},"accepts_product_review_abandoned_cart_emails":true,"store_credit_amounts":{"amount":12.34},"origin_channel_id":1,"channel_ids":[1],"attributes":{"name":"gp_customer_id","value":"2112"}}
My code:
$insertStr =json_encode([
'email' => '[email protected]',
'first_name' => 'Lucy',
'last_name' => 'van Pelt',
'company' => 'Peanuts LTD',
'phone' => '2345551234',
'notes' => '',
'tax_exempt_category' => '',
'customer_group_id' => 0,
'addresses' => [
'address1' => '120 Great Pumpkin Ln',
'address2' => '',
'address_type' => 'residential',
'city' => 'St Paul',
'company' => 'Peanuts LTD',
'country_code' => 'US',
'first_name' => 'Lucy',
'last_name' => 'van Pelt',
'phone' => '2345551234',
'postal_code' => '55101',
'state_or_province' => 'Minnesota',
'form_fields' => [
'name' => 'Are your sales tax exempt? ',
'value' => 'No'
]
],
'authentication' => [
'force_password_reset' => false,
'New_password' => 'PeanutsLTD'
],
'accepts_product_review_abandoned_cart_emails' => true,
'store_credit_amounts' => [ 'amount' => 12.34],
'origin_channel_id' => 1,
'channel_ids' => [1],
'attributes' => [
'name' => 'gp_customer_id',
'value' => '2112'
]
]);
CodePudding user response:
You're encoding a PHP associative array. That's encoded as a JSON object.
As the error message from the API says, it's expecting an array, not an object. You need to wrap the associative array inside an ordinary array. So add another set of []
around it.
You have to do the same thing with the addresses
and attributes
arrays.
$insertStr =json_encode([[
'email' => '[email protected]',
'first_name' => 'Lucy',
'last_name' => 'van Pelt',
'company' => 'Peanuts LTD',
'phone' => '2345551234',
'notes' => '',
'tax_exempt_category' => '',
'customer_group_id' => 0,
'addresses' => [[
'address1' => '120 Great Pumpkin Ln',
'address2' => '',
'address_type' => 'residential',
'city' => 'St Paul',
'company' => 'Peanuts LTD',
'country_code' => 'US',
'first_name' => 'Lucy',
'last_name' => 'van Pelt',
'phone' => '2345551234',
'postal_code' => '55101',
'state_or_province' => 'Minnesota',
'form_fields' => [
'name' => 'Are your sales tax exempt? ',
'value' => 'No'
]]
],
'authentication' => [
'force_password_reset' => false,
'New_password' => 'PeanutsLTD'
],
'accepts_product_review_abandoned_cart_emails' => true,
'store_credit_amounts' => [ 'amount' => 12.34],
'origin_channel_id' => 1,
'channel_ids' => [1],
'attributes' => [[
'name' => 'gp_customer_id',
'value' => '2112'
]]
]]);
CodePudding user response:
It is not valid PHP syntax.
Depending on the semantic of the data you want to encode, the outer {
and }
either are not needed or they must be replaced by [
and ]
.
If the data is a list of users then it should be like this:
json_encode([['email' => '[email protected]', ...], ['email' => '[email protected]', ...]]);
It is an array of arrays.
If the data represents a single user then it should be only ['email' => '...', ...]
(no outer {
and }
).
For Addresses
, the plural tells that it is a list, so the curly braces ({
and }
) must be replaced by square brackets ([
and ]
) to represent a list containing only one array.
I think it should be like this:
$data = [
[
'email' => '[email protected]',
'firstname' => 'peter',
'last_name' => 'griffin',
'company' => 'Nantuket Brewing',
'Addresses' => [
[
'addr1' => '31 Spooner Ln',
'city' => 'Quahog',
'state' => 'RI'
],
// possible more addresses here
],
],
// possible more items here
// [
// 'email' => '[email protected]',
// ...
// ]
];
$asString = json_encode($data);
See it online.
In PHP an array can be used as a JavaScript/C/C /etc array, with numeric indices starting from 0 or it can be used as a JavaScript object, when its keys are strings.
The outer array ($data
) is a standard array, its keys are numbers starting from 0
. Each of its items is an associative array; their keys are strings ('email'
, 'firstname'
and so on).
The value of property Addresses
of each item is an array indexed by numbers. Each of its items (each address) are associative arrays.
PHP encodes the arrays that have numeric consecutive keys starting from 0
as arrays in JSON. All the other PHP arrays (associative arrays, arrays that have numeric keys with gaps, not in order or with negative indices) are encoded as objects in JSON.
--
The code above produces a JSON equivalent to the value of CURLOPT_POSTFIELDS
that you put in the question. By default json_encode()
produces a compact JSON, without unneeded spaces.
It can be told to produce a pretty formatted JSON (by passing JSON_PRETTY_PRINT
as its second argument) but a pretty printed JSON is useful only for debugging. The destination server should not care about the formatting and it only increases the JSON's size.