5

I have a JSON file and I would like to print that object in JSON:

JSON

[{"text": "Aachen, Germany - Aachen/Merzbruck (AAH)"}, {"text": "Aachen, Germany - Railway (ZIU)"}, {"text": "Aalborg, Denmark - Aalborg (AAL)"}, {"text": "Aalesund, Norway - Vigra (AES)"}, {"text": "Aarhus, Denmark - Aarhus Airport (AAR)"}, {"text": "Aarhus Limo, Denmark - Aarhus Limo (ZBU)"}, {"text": "Aasiaat, Greenland - Aasiaat (JEG)"}, {"text": "Abadan, Iran - Abadan (ABD)"}]

I have tried with following method,

<?php   
  $jsonurl='http://website.com/international.json'; 
  $json = file_get_contents($jsonurl,0,null,null);  
  $json_output = json_decode($json);        
  foreach ($json_output as $trend)  
  {         
   echo "{$trend->text}\n";     
  } 
?>

but it didn't work:

Fatal error: Call to undefined function var_dup() in /home/dddd.com/public_html/exp.php on line 5

Can anyone help me understand what I'm doing wrong?

David Buck
  • 3,752
  • 35
  • 31
  • 35
user123456789
  • 556
  • 3
  • 12
  • 31
  • did you `var_dump($json_output);` to see what you get? And to see whether json_decode makes an array out of your data? – herrhansen Jun 25 '13 at 08:55
  • 2
    "It does not work" is not a valid PHP error message though. – BMN Jun 25 '13 at 08:56
  • @quidage meant `var_dump($json_output)` - or alternatively simply `print_r($json_output)` – Aleks G Jun 25 '13 at 08:56
  • These links might help you: http://stackoverflow.com/questions/9138625/how-to-process-json-in-php http://stackoverflow.com/questions/9597941/how-to-echo-json-in-php – JunM Jun 25 '13 at 08:56
  • Fatal error: Call to undefined function var_dup() in /home/dddd.com/public_html/exp.php on line 5 – user123456789 Jun 25 '13 at 08:58
  • These links might help you: http://stackoverflow.com/questions/9138625/how-to-process-json-in-php http://stackoverflow.com/questions/9597941/how-to-echo-json-in-php – JunM Jun 25 '13 at 08:58
  • i meant `var_dump()` as i edited before – herrhansen Jun 25 '13 at 08:59
  • Your json decoding and loop is fine. That means there must be a problem retrieving the JSON with your `file_get_contents`. See this demo http://codepad.org/6vNqGVqz – MrCode Jun 25 '13 at 09:11
  • Start with `var_dump($json)` to see what file_get_contents returns. Why are you setting `0,null,null` in the call? That won't do anything useful. – MrCode Jun 25 '13 at 09:13
  • i have tried but this error is coming Warning: Invalid argument supplied for foreach() in /home/website/public_html/exp.php on line 6 – user123456789 Jun 25 '13 at 09:17
  • try to put a `exit();` or `return ;` before your `json_decode` call. – BMN Jun 25 '13 at 09:22
  • nothing is happend...@Yellow Bird – user123456789 Jun 25 '13 at 09:27

5 Answers5

6
<?php   

  $jsonurl='http://website.com/international.json'; 
  $json = file_get_contents($jsonurl,0,null,null);  
  $json_output = json_decode($json, JSON_PRETTY_PRINT); 
  echo $json_output;
?>

by using JSON_PRETTY_PRINT u transform your json to pretty formatting, using json_decode($json, true) doesn't reformat your json to PRETTY formatted output, also you don't have to run loop over all keys to export same JSON object again, you could use those constants also which could clean up your json object before exporting it.

json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
Mortgy
  • 543
  • 5
  • 9
  • 1
    While this answer is probably correct and useful, it is preferred if you [include some explanation along with it](http://meta.stackexchange.com/q/114762/159034) to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked. Thanks! – Hatchet Mar 18 '16 at 13:31
  • thanks for your comment, i will update my answer with more explanation :) – Mortgy Apr 17 '16 at 11:14
  • @Mortgy this doesn't work with json_decode directly, you need something like this: `json_encode(json_decode($json_ugly), JSON_PRETTY_PRINT);` taken from: [link](https://stackoverflow.com/a/33226257/164374) – Vlax Aug 09 '18 at 12:14
3

use

$json_output = json_decode($json, true);

by default json_decode give OBJECT type but you are trying to access it as Array, so passing true will return an array.

Read documentation : http://php.net/manual/en/function.json-decode.php

Sumit Gupta
  • 2,152
  • 4
  • 29
  • 46
0

Try this code:

<?php   
  $jsonurl='http://website.com/international.json'; 
  $json = file_get_contents($jsonurl,0,null,null);  
  $json_output = json_decode($json, true);        
  foreach ($json_output as $trend){         
   echo $trend['text']."\n";     
  } 
?>

Thanks, Dino

dino.keco
  • 1,401
  • 1
  • 12
  • 18
0
$data=[{"text": "Aachen, Germany - Aachen/Merzbruck (AAH)"}, {"text": "Aachen, Germany - Railway (ZIU)"}, {"text": "Aalborg, Denmark - Aalborg (AAL)"}, {"text": "Aalesund, Norway - Vigra (AES)"}, {"text": "Aarhus, Denmark - Aarhus Airport (AAR)"}, {"text": "Aarhus Limo, Denmark - Aarhus Limo (ZBU)"}, {"text": "Aasiaat, Greenland - Aasiaat (JEG)"}, {"text": "Abadan, Iran - Abadan (ABD)"}]
$obj = json_decode($data);
$text = $obj[0]->text;

This will work.

0

JSON_FORCE_OBJECT in your json call eg :

$obj = json_decode($data);

Instead write like this:

$obj = json_decode($data, JSON_FORCE_OBJECT);
tjheslin1
  • 1,378
  • 6
  • 19
  • 36
  • Hi Vishal. I've made a suggested edit to your answer to format the code. If possible could you also provide details as to what the fix is doing? Thanks! – tjheslin1 Feb 19 '20 at 13:40