I'm trying to decode the following json which I have checked with jsonlint and returns that it is a valid json:
[{"gatewayId": "5154979870408704", "sensorId": "5698497110081536"}, {"timestamp": "20160513T225959000Z", "tagId": "123456789AB1", "io": "in"}, {"timestamp": "20160513T235959000Z", "tagId": "123456789AB2", "io": "out"}]
However the result is always Syntax Error when I call json_last_error_msg() Here is my code:
$param = substr($HTTP_RAW_POST_DATA, 1);
$param =utf8_encode($param);
$json = json_decode($param,true);
echo json_last_error_msg();
var_dump($param);
The json is being sent to me using curl which is why it is in $HTTP_RAW_POST_DATA, and I'm removing the first character because when making a var_dump I noticed a blank character there, however that's not the problem. Any help is appreciated. EDIT: This is the result of var_dump($HTTP_RAW_POST_DATA)
string(187) " [{gatewayId: 5154979870408704, sensorId: 5698497110081536}, {timestamp: 20160513T225959000Z, tagId: 123456789AB1, io: in}, {timestamp: 20160513T235959000Z, tagId: 123456789AB2, io: out}]"
EDIT2: So turns out it was because the double quotes were being removed in the curl request that had the payload like
-d "[{"gatewayId": "5154979870408704", "sensorId": "5698497110081536"}, {"timestamp": "20160513T225959000Z", "tagId": "123456789AB1", "io": "in"}, {"timestamp": "20160513T235959000Z", "tagId": "123456789AB2", "io": "out"}]"
I replaced the double quotes with single quotes in the begining and end of the payload and now its working fine, just leaving this here in case anyone has the same problem. new payload:
-d '[{"gatewayId": "5154979870408704", "sensorId": "5698497110081536"}, {"timestamp": "20160513T225959000Z", "tagId": "123456789AB1", "io": "in"}, {"timestamp": "20160513T235959000Z", "tagId": "123456789AB2", "io": "out"}]'
Thanks!