diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc
index 6b9b850ca..7d6090e0a 100644
--- a/src/libutil/logging.cc
+++ b/src/libutil/logging.cc
@@ -163,7 +163,7 @@ struct JSONLogger : Logger {
 
     void write(const nlohmann::json & json)
     {
-        prevLogger.log(lvlError, "@nix " + json.dump());
+        prevLogger.log(lvlError, "@nix " + json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace));
     }
 
     void log(Verbosity lvl, const FormatOrString & fs) override