H
H
hitakiri2014-09-03 17:03:54
PHP
hitakiri, 2014-09-03 17:03:54

How to generate a file from an array of data in JSON?

Good time.
I am looking for a script, framework or other solution for the task of converting data from json to files.
I give a simplified example:
There is a .json file of this kind

[
   {
      "name":"The Shawshank Redemption",
      "year":"1994",
      "stars":[
         "Tim Robbins",
         "Morgan Freeman",
         "Bob Gunton"
      ]
   },
   {
      "name":"The Godfather",
      "year":"1972",
      "stars":[
         "Marlon Brando",
         "Al Pacino",
         "James Caan"
      ]
   }
]

At the output, two files should be created in an arbitrary folder (for example, movies):
movies\The Shawshank Redemption.txt - entry in the file: 1994
movies\The Godfather.txt - entry in the file: 1972
I.e. the "name" key is the name of the file, and the value of the "year" key is written to the file.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2014-09-03
Protko @Fesor

Lord, like that?

$data = json_encode($json);

foreach ($data as $movie) {
    file_put_content('/path/to/dir'/ . $movie['name'] . '.txt', $movie['year'];
}

H
hitakiri, 2014-09-03
@hitakiri

Yes, sort of. The only problem is that most of the keys are arbitrary, there is a lot of data, so I'm looking for, suddenly there is a tool for such tasks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question