Can you help me figure out this notation:
$myArray = jason_encode( [{ '
'email' => 'a@bc.com',
'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\": \"string@example.com\",\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":"lucy@peanuts.mil","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' => 'lucy@peanuts.mil',
'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'
]
]);
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' => 'lucy@peanuts.mil',
'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'
]]
]]);