PATCH api request returns json error

Hi, I’ve been struggling to make a PATCH curl request through PHP but it’s not working. I’ve tried various combinations and searched the forums for examples and ideas but I keep getting

{“errors”:{“error”:“JSON: Syntax error”}}

Here’s a short version of my code :

	$data["dcterms:format"] = array(
						   array(
							"type" => "customvocab:8",
							"property_id" => 9,
							"property_label" => "Format",		
							"is_public" => 1,
							"@value" => $technique_el,
							"@language" => "el"
							),			
							array(
							"type" => "customvocab:22",			
							"property_id" => 9,
							"property_label" => "Format",		
							"is_public" => 1,
							"@value" => $technique_en,
							"@language" => "en"
							 )
						);
	$JSONdata = "[".json_encode($data)."]";   

	$ch = curl_init();
	$headers = array('Content-type: application/json');			
	curl_setopt_array($ch, array(
	  CURLOPT_URL => 'https://dam.momus.gr/api/items/1098/?key_identity=xxx&key_credential=xxx',
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => '',
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 0,
	  CURLOPT_FOLLOWLOCATION => true,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => 'PATCH',
	  CURLOPT_POSTFIELDS => $JSONdata, 
	  CURLOPT_HTTPHEADER => $headers,
	));
	$response = curl_exec($ch);
	echo '<pre>'; print_r($response); echo '</pre>'; 

        curl_close($ch);

I can’t seem to find where the problem is. I welcome any ideas…

If we’re returning “JSON: Syntax error” then there’s a syntax error in the JSON payload that you’re sending.

I don’t see anything immediately wrong with your code (though the outer brackets you’re adding to the json_encode return are a strange choice and could cause different problems).

With your actual code, have you output the JSON string that you’re sending and double-checked that it’s valid?

Yes I have, the array output is:

Array
(
    [dcterms:format] => Array
        (
            [0] => Array
                (
                    [type] => customvocab:8
                    [property_id] => 9
                    [property_label] => Format
                    [is_public] => 1
                    [@value] => Some Value
                    [@language] => el
                )

            [1] => Array
                (
                    [type] => customvocab:22
                    [property_id] => 9
                    [property_label] => Format
                    [is_public] => 1
                    [@value] => Another value
                    [@language] => el
                )

        )

)

and the JSON is:

[{"dcterms:format":[{"type":"customvocab:8","property_id":9,"property_label":"Format","is_public":1,"@value":"Some value","@language":"el"},{"type":"customvocab:22","property_id":9,"property_label":"Format","is_public":1,"@value":"Another value","@language":"en"}]}]

which seems ok to me. Also I have tried with and without the brackets and I get the same error.

Ok I figured it out. There were 2 problems: I was outputting the response which was not a json so I kept getting json syntax error (so my json string was fine). The response was “Temporary Redirect” because I had an extra slash / on my url, just after the item id.