0

Im trying to decode the following json data,
NOTE, the data has a weird attribute a '\' in front of each key.

{"data":"{\"product_data\":{\"LXR12-3\":0,\"GXPI01\":1,\"LXR12-1\":1,\"LXR12-2\":1},\"access_token\":\"1M6opdQlMXygxowkn8uOgsEYNUGcXKF4UcuMX0fzwpxmZT4Hn7\",\"intent\":\"okay\",\"update\":false}"}

PHP

$jsonRaw = file_get_contents('php://input');
file_put_contents('RAW.txt', $jsonRaw);
$data = json_decode($jsonRaw,true);
file_put_contents('DATA.txt', $data);
$access_token = $data['access_token'];  
$intent = $data['intent'];  
$isEdit = $data['update'];  


$text = "access_token : $access_token | intent : $intent | edit : $isEdit";
file_put_contents('GOT_DATA.txt', $text);

The output of the 'GOT_DATA' file is,

access_token :  | intent :  | edit :  

The output for the DATA.txt file is,

{"product_data":{"LXR12-3":0,"GXPI01":1,"LXR12-1":1,"LXR12-2":1},"access_token":"1M6opdQlMXygxowkn8uOgsEYNUGcXKF4UcuMX0fzwpxmZT4Hn7","intent":"okay","update":false}

The values are not begin stored into the variables? What exactly am i doing wrong here?
and does anyone know why the recvied json have a '\' in front?

UPDATE

After changing the source of the JSON i managed to return it like (RAW.txt),

{"data":{"product_data":{"LXR12-2":0,"GXPI01":0,"LXR12-3":1,"LXR12-1":1},"access_token":"1M6opdQlMXygxF4UcuMX0fzwpxmZT4Hn7","intent":"okay","update":false}}

Now the outputs for the files for DATA.txt,

Array

It just says 'Array' Why???

Elinoter99
  • 599
  • 1
  • 7
  • 22
  • You have `{"data":"...` - that final quote means that the value you stored in the data property was stringified first, so data contains a string not an object. I imagine you wanted that to be an object, so whatever code is posting this to php, it's encoding the data property as a string first - make it stop. – James Feb 01 '18 at 18:46
  • Note you've just posted your access token. There doesn't appear to be any other identifying information in the post, but you probably want to change/revoke it immediately anyway. – Alex Howansky Feb 01 '18 at 18:52
  • @James im going through my code now, will update you asap. – Elinoter99 Feb 01 '18 at 19:00
  • @AlexHowansky I changed some letters so it'll be okay :P – Elinoter99 Feb 01 '18 at 19:00
  • @James I updates the question and fixed what you mentioned eariler, but now all i get is 'Array' as outputafter decoding – Elinoter99 Feb 01 '18 at 19:34
  • Now you have product_data as the only property of data. So $data["intent"] won't work, it would have to be $data["product_data"]["intent"] – James Feb 01 '18 at 19:49
  • OMG, how did i miss that. It works now! Thanks – Elinoter99 Feb 01 '18 at 19:53

0 Answers0