-4

I have json file which contains multiple json objects. Example

{"t":"abc-1","d":"2017-12-29 12:42:53"}
{"t":"abc-2","d":"2017-12-29 12:43:05"}
{"t":"abc-3","d":"2017-12-30 14:42:09"}
{"t":"code-4","d":"2017-12-30 14:42:20"}

Want to read this file and store into database, but I couldn't convert json to php array which further I can store into database.

I tried json_decode function, but its not working. I search for this but in every link its showing use json_decode. Below is my code

$filename = "folder/filename.json";
$data = file_get_contents($filename);
echo $data;
$tags = json_decode($data, true);
echo"<pre>";print_r($tags);exit;

$data is echoed but not the $tags.

Thanks in advance.

Nagesh Katke
  • 540
  • 7
  • 23

6 Answers6

1

Make array of objects and use it later

$j = array_map('json_decode', file('php://stdin'));
print_r($j);

demo

splash58
  • 26,043
  • 3
  • 22
  • 34
  • Not that it really matters, but array_map is actually slower than just looping the array. – Andreas Dec 30 '17 at 10:31
  • I guess so. I just read this thread a while ago. https://stackoverflow.com/questions/18144782/performance-of-foreach-array-map-with-lambda-and-array-map-with-static-function and according to those tests foreach is faster or about equal on all accounts. But I guess it can depend then... – Andreas Dec 30 '17 at 10:49
0

If it's only four lines you can explode and json_decode each line and add it to an array.

$s = '{"t":"abc-1","d":"2017-12-29 12:42:53"}
{"t":"abc-2","d":"2017-12-29 12:43:05"}
{"t":"abc-3","d":"2017-12-30 14:42:09"}
{"t":"code-4","d":"2017-12-30 14:42:20"}';

$arr = explode(PHP_EOL, $s);
Foreach($arr as $line){
    $json[] = json_decode($line,true);
}
Var_dump($json);

https://3v4l.org/97m0E

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • no it can be more than 4 lines as its generating dynamically. – Nagesh Katke Dec 30 '17 at 09:28
  • @Nagesh it works either way. But if it's more lines it will take slightly longer to finnish. Why did you downvote this? – Andreas Dec 30 '17 at 09:33
  • Can the little coward who downvoted give a comment? What's the reason to downvote all answers here? OP has a clear question and an attempt. What more do ask for? – Andreas Dec 30 '17 at 09:38
0

Multiple objects in a row should be enclosed in a json array and separated with comma like elements.So you need a [ ] at the start and end of the file.Also you could close the pre tag

Either you should fix the file generating that 'json' or you can use fgets to get one line at a time, and use json decode on every line

dac1n
  • 309
  • 2
  • 11
0

As pointed by other, JSON which you shared isn't valid. And, I think, it is stored in your file in same fashion. I would suggest to read this file line by line each line then you can decode.

$handle = fopen("folder/filename.json", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {

        $tags = json_decode($line, true);
        echo"<pre>";print_r($tags);exit;
    }

    fclose($handle);
} else {
    // error opening the file.
} 
Ravi
  • 30,829
  • 42
  • 119
  • 173
0
Assuming a file called `filename.json` contains the following lines

{"t":"abc-1","d":"2017-12-29 12:42:53"}
{"t":"abc-2","d":"2017-12-29 12:43:05"}
{"t":"abc-3","d":"2017-12-30 14:42:09"}
{"t":"code-4","d":"2017-12-30 14:42:20"}

So each one is a separate json entity

$filename = "folder/filename.json";
$lines=file( $filename );
foreach( $lines as $line ){
    $obj=json_decode( $line );
    $t=$obj->t;
    $d=$obj->d;
    /* do something with constituent pieces */
    echo $d,$t,'<br />';
}
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
-2

Your JSON is invalid, as it has multiple root elements

Fixing it like the following should work (note the [, ] and commas):

[
    {"t":"abc-1","d":"2017-12-29 12:42:53"},
    {"t":"abc-2","d":"2017-12-29 12:43:05"},
    {"t":"abc-3","d":"2017-12-30 14:42:09"},
    {"t":"code-4","d":"2017-12-30 14:42:20"}
]

If you cannot influence how the JSON file is created, you will need to create your own reader, as PHP is not built to support invalid formatting. You could separate the file by new lines and parse each one individually.

Daan
  • 578
  • 7
  • 21