0

I have this code in a.json:

{
  "body" : {
    "outboundSMSMessageRequest": {
      "address": [
        "9456654978" /* 1 */
      ],
      "senderAddress": " 64735 ", /* 3 */
      "outboundSMSTextMessage": {
        "message": "Welcome to fgf  Your Confirmation Code - " /* 2 */
      },
      "clientCorrelator": "1", /* 4 */
      "receiptRequest": {
        "notifyURL": "2", /* 5 */
        "callbackData": "3" /* 6 */
      },
      "senderName": "4" /* 7 */
    }
  }
}

I want to print the values as marked above (1, 3, 2, 4, 5, 6, 7, the values only). I have tried this:

$jsonData = file_get_contents("a.json");

$json = json_decode($jsonData,true);

echo $json;

but I end up with the following notice:

PHP Notice: Array to string conversion

How can I print the values as desired?

Script47
  • 14,230
  • 4
  • 45
  • 66
Amer Abo Tbk
  • 25
  • 1
  • 5
  • 1
    `echo` expects a string, `$json` is an array at that point. Try `print_r($json);` instead. – rickdenhaan Aug 11 '17 at 12:20
  • 2
    Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – localheinz Aug 11 '17 at 13:10

2 Answers2

0

Presumably by:

I want to in a PHP page to print (1, 3, 2, 4, 5, 6, 7, the values only)

... you mean those values you've marked with the numbered comments, e.g. /* 1 */ - if so, this should do the trick (to just echo it out anyway).

$jsonData = file_get_contents("a.json");
$o = json_decode($jsonData);

$outboundSMSRequest = $o->body->outboundSMSMessageRequest;
echo $outboundSMSRequest->address[0] . "\n"                         // outbound address
    . $outboundSMSRequest->outboundSMSTextMessage->message . "\n"   // outbound message
    . $outboundSMSRequest->senderAddress . "\n"                     // sender address
    . $outboundSMSRequest->clientCorrelator . "\n"                  // client correlator
    . $outboundSMSRequest->receiptRequest->notifyURL . "\n"         // notification URL
    . $outboundSMSRequest->receiptRequest->callbackData . "\n"      // callback data
    . $outboundSMSRequest->senderName;                              // sender name


When you use json_decode() you turn the JavaScript object into a PHP object - so you just access it accordingly.

CD001
  • 8,332
  • 3
  • 24
  • 28
-1

Json do not support comments so you need to remove them from your file, see Can comments be used in JSON?

rypskar
  • 2,012
  • 13
  • 13
  • 1
    Not related to the problem. If the JSON could not be decoded, PHP would not have thrown an "Array to string conversion" notice. – rickdenhaan Aug 11 '17 at 12:23
  • i want in php page to print or display (1,3,2,4,5,6,7) (values only) i tried many ways to fix it . – Amer Abo Tbk Aug 11 '17 at 12:25
  • @rickdenhaan that is true, but you had already added a comment that should solve that part :) – rypskar Aug 11 '17 at 12:25
  • @AmeerAboTbk then you need to pick the values you want from the object you get back from json_decode and echo them. Using print_r as other have suggested to see the data you get should point you in the right direction – rypskar Aug 11 '17 at 12:29