0

I have a json file from import.io that returns null when decoded, but shows up as a string when encoded and is all there. How can I "loop" through a json string in PHP?

Json data is very lengthy so I refrained from posting it.

Json: https://codeshare.io/2BD4ma

Code:

<?php

$jsonFile = file_get_contents('feeds/quotes.json');

//decode
$results = json_encode($jsonFile, TRUE);

var_dump($results);


?>

1 Answers1

1

It would be nice to see the code you are using, or have tried....

Either way, you need to use json_decode on the JSON object, which will turn it into a PHP array:

$data = json_decode($yourJsonData);

// print_r it to see:
print_r($data);

// to loop through it, you could do:
foreach ($data as $item)
{
    print_r($item); // used print_r: unsure if this data will contain nested objects/arrays
}
Stuart
  • 6,630
  • 2
  • 24
  • 40
  • I've tried this, but the data is null everytime I use decode. – user1721449 Nov 23 '16 at 12:47
  • THat doesnt look like properly formatted JSON. Are you sure its not been serialized / escaped somewhere in the process..? – Stuart Nov 23 '16 at 12:50
  • Not sure there is anyway to tell. It is automatically generated by import.io – user1721449 Nov 23 '16 at 12:51
  • Can you post the endpoint / URL that you are getting the JSON from..? – Stuart Nov 23 '16 at 12:52
  • here you go: http://importio.desk.com/customer/en/portal/articles/2471525-json-response and https://data.import.io/extractor/eff120be-7eec-4211-a808-53f12d46353a/json/latest?_apikey=bfdc92eb3b02401da6c1213e42161835f41855043513e60ac53a96e9c3c17ffdee65f3b4ed5a6e0ebeeea3a741c25573e8891bf1df067e478a4f0c96a44124dcd7c6c95148299d9f3c89fe3d7b9c45a6 – user1721449 Nov 23 '16 at 12:53
  • Ok, so the data returned from data.import.... is perfectly valid JSON, you should have no trouble in using `json_decode` there. In your example, where does feeds/quotes.json get its data from? The same place you just posted? – Stuart Nov 23 '16 at 12:56
  • the same file; it is just local to the server rather than being fetched each time – user1721449 Nov 23 '16 at 12:58
  • Interesting, because the output of data.import.io... is different to JSON in the codeshare you posted. i.e if you go to the codeshare URL and save the json file, then run json_decode on it, it will work... – Stuart Nov 23 '16 at 13:00
  • using the method in the article that I posted yields roughly the same results - I get unexpected token errors – user1721449 Nov 23 '16 at 13:06
  • safe to say i am not using node either. – user1721449 Nov 23 '16 at 13:08
  • may give up on this one; can't wrap my head around how to get it to parse correctly. – user1721449 Nov 23 '16 at 13:29
  • It seem, as though the json you are getting is a bunch of json objects separated by new lines. Looking at the docs (http://importio.desk.com/customer/en/portal/articles/2471525-json-response) they also mention this too. Try splitting the data you get on new lines, then json_decode each item? – Stuart Nov 23 '16 at 18:48
  • any idea how I can go about achieving this? Just need a point in the right direction at least. – user1721449 Dec 01 '16 at 07:44
  • Also, what is the separator for each of these lines? – user1721449 Dec 01 '16 at 08:12