V
V
vaflya2019-08-21 15:04:05
MySQL
vaflya, 2019-08-21 15:04:05

How to parse JSON in MySQL?

Good afternoon, there are 2 tables:
No. 1 films -> id, title, summary
No. 2 donor -> field,...., data
is in the donor table, the last field contains information about the film in the form of a json array
, for example:

{'title': 'Женись на мне, чувак', 'title_en': 'Épouse-moi mon pote', 'year': 2017, 'tagline': 'Дружба - настоящая. Брак - фиктивный', 'description': 'Единственный шанс для провалившего экзамен студента остаться в Париже – это фиктивный брак. Однако, найти невесту так быстро практически невозможно. И тогда на помощь приходит лучший друг.', 'poster_url': 'https://st.kp.yandex.net/images/film_iphone/iphone360_999563.jpg', 'duration': 92, 'countries': ['Франция'], 'genres': ['комедия'], 'kinopoisk_rating': 6.6, 'kinopoisk_votes': 5762, 'imdb_rating': 6.1, 'imdb_votes': 2150, 'premiere_ru': '2017-11-09', 'premiere_world': '2017-10-25', 'minimal_age': 18, 'actors': ['Тарек Будали', 'Филипп Лашо', 'Шарлотта Габрис', 'Анди', 'Давид Марсе', 'Жюльен Аррути', 'Байа Белаль', 'Филипп Дюкен', 'Зинедин Суалем', 'Дуду Маста'], 'directors': ['Тарек Будали'], 'producers': ['Кристоф Сервони', 'Марк Фисцман', 'Кристиан Бомар'], 'writers': ['Тарек Будали', 'Пьер Дудан'], 'composers': ['Максим Депре', 'Мишель Торджман'], 'editors': ['Антуан Варей'], 'designers': ['Сэмюэл Тессер', 'Тьерри Пулет', 'Тьерри Руксель'], 'operators': ['Антуан Марто']}

I need to scatter this data over the films table, tell me
how this can be done, or mysql will not be able to work with JSON and needs to be parsed through PHP?
Through php, I made it handicraft like this:
for ($i=0; $i < 39553; $i++) { 
      $result = $this->db->query('SELECT `material_data`, `kinopoisk_id` FROM `x` WHERE `id` = '.$i.' LIMIT 1');

      while ($row = $result -> fetch ()) {
                                $pre = utf8_decode($row['material_data'])
        $json = json_decode($pre);

        if (!empty($json['description']) and !empty($row['kinopoisk_id'])) {
          $this->db->query('UPDATE `films` SET `summary` = '.$json['description'].' WHERE `kp_id` = '.$row['kinopoisk_id']);
        }

      }


    }

It's strange, but after decoding the data array turns into NULL NULL NULL NULL NULL
Checked jsons selectively, all valid
Added utf8_decode(), didn't help either

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vaflya, 2019-08-21
@vaflya

The answer is: if a single quote [ ' ] is used in the JSON array, then json_decode() does not digest this array,
then we change all single quotes to double ones

$prejson = str_replace('\'', '"', $row['material_data']);
        $json = json_decode($prejson, true);
        var_dump($json['description']);

and everything is in order, thanks to everyone who answered.

T
ThunderCat, 2019-08-21
@ThunderCat

if this needs to be done 1 time, IMHO there is no point in bothering and it’s easier to drive everything through the puff.
if it's purely interesting to have sex through a muscle - https://dev.mysql.com/doc/refman/8.0/en/json-attri...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question