jsonshopify

Shopify metafield is returning an error of "value":"expected Hash to be a String" when the metafield type is json


I'm using the Shopify customer API to create customers. It all works well until I add a metafield of type json. Then, I get the response:

{"errors":{"value":"expected Hash to be a String"}}.

Here is the entire object I'm submitting:

{
    "customer": {
        "accepts_marketing": "true",
        "accepts_marketing_updated_at": "2022-03-01T12:45:42.770",
        "currency": "USD",
        "default_address": {
            "zip": "91321",
            "city": "SomeCity",
            "phone": "5555551111",
            "customerCompany": "",
            "countryCode": "US",
            "default": "true",
            "address1": "1111 My Street",
            "address2": "",
            "province_code": "CA",
            "last_name": "LastusNamus",
            "first_name": "FirstusNamus"
        },
        "email": "myemail@yahoo.com",
        "first_name": "FirstusNamus",
        "last_name": "LastusNamus",
        "created_at": "2011-12-07T00:00:00",
        "marketing_opt_in_level": "single_opt_in",
        "note": "",
        "order_count": 0,
        "phone": "5555551111",
        "state": "enabled",
        "tax_exempt": "false",
        "verfified_email": "true",
        "metafields": [{
            "namespace": "customer",
            "key": "kickeeinfo",
            "type": "json",
            "value": {
                "idcustomer": "37",
                "iRewardPointsAccrued": "3000",
                "iRewardPointsUsed": "0",
                "idCustomerCategory": "0"
            }
        }]
    }
}

Here is the string actually being submitted:

{"customer":{"accepts_marketing":"true","accepts_marketing_updated_at":"2022-03-01T12:45:42.770","currency":"USD","default_address":{"zip":"91321","city":"SomeCity","phone":"5555551111","customerCompany":"","countryCode":"US","default":"true","address1":"1111 My Street","address2":"","province_code":"CA","last_name":"LastusNamus","first_name":"FirstusNamus"},"email":"myemail@yahoo.com","first_name":"FirstusNamus","last_name":"LastusNamus","created_at":"2011-12-07T00:00:00","marketing_opt_in_level":"single_opt_in","note":"","order_count":0,"phone":"5555551111","state":"enabled","tax_exempt":"false","verfified_email":"true","metafields":[{"namespace":"customer","key":"kickeeinfo","type":"json","value":{"idcustomer":"37","iRewardPointsAccrued":"3000","iRewardPointsUsed":"0","idCustomerCategory":"0"}}]}}

What is the API expecting to be submitted? I've tried escaping the metafield "value" object and that doesn't work. I get an "unexpected token" error at that point.


Solution

  • The metafield value has to be escaped and then enclosed in quotes:

        {
        "customer": {
            "accepts_marketing": "true",
            "accepts_marketing_updated_at": "2022-03-01T12:45:42.770",
            "currency": "USD",
            "default_address": {
                "zip": "91321",
                "city": "SomeCity",
                "phone": "5555551111",
                "customerCompany": "",
                "countryCode": "US",
                "default": "true",
                "address1": "1111 My Street",
                "address2": "",
                "province_code": "CA",
                "last_name": "LastusNamus",
                "first_name": "FirstusNamus"
            },
            "email": "myemail@yahoo.com",
            "first_name": "FirstusNamus",
            "last_name": "LastusNamus",
            "created_at": "2011-12-07T00:00:00",
            "marketing_opt_in_level": "single_opt_in",
            "note": "",
            "order_count": 0,
            "phone": "5555551111",
            "state": "enabled",
            "tax_exempt": "false",
            "verfified_email": "true",
            "metafields": [{
                "namespace": "customer",
                "key": "kickeeinfo",
                "type": "json",
                "value": "{\"idcustomer\": \"37\",\"iRewardPointsAccrued\": \"3000\",\"iRewardPointsUsed\": \"0\",\"idCustomerCategory\": \"0\"}"
            }]
        }
    }