{"id": 1, "nickname": "joker1007", "comments": [{"id": 1, "body": "comment body"}, {"id": 2, "body": "next comment"}] SELECT CONCAT( '{', '"id":',users.id, ',"nickname":',IF(users.nickname, CONCAT('"', users.nickname, '"'), "null"), ',"comments":',CONCAT('[', GROUP_CONCAT((CONCAT('{"id":', comments.id), CONCAT(',"body":"',comments.body,'"}')), ']'), '}' ) AS payload FROM users INNER JOIN comments