diff --git a/src/cyclictest/cyclictest.c b/src/cyclictest/cyclictest.c
index 033b95a3a19a..59dda1973b1a 100644
--- a/src/cyclictest/cyclictest.c
+++ b/src/cyclictest/cyclictest.c
@@ -2135,7 +2135,7 @@ int main(int argc, char **argv)
 		printf("\033[%dB", num_threads + 2);
 
 	if (strlen(outfile) != 0)
-		rt_write_json(outfile, write_stats, NULL);
+		rt_write_json(outfile, ret, write_stats, NULL);
 
 	if (quiet)
 		quiet = 2;
diff --git a/src/include/rt-utils.h b/src/include/rt-utils.h
index 42c2772262db..f07b57d5f7c1 100644
--- a/src/include/rt-utils.h
+++ b/src/include/rt-utils.h
@@ -84,7 +84,7 @@ void rt_init(int argc, char *argv[]);
 
 void rt_test_start(void);
 
-void rt_write_json(const char *filename,
+void rt_write_json(const char *filename, int return_code,
 		   void (*cb)(FILE *, void *),
 		   void *data);
 
diff --git a/src/lib/rt-utils.c b/src/lib/rt-utils.c
index efb5d1d9e866..d264c348ad42 100644
--- a/src/lib/rt-utils.c
+++ b/src/lib/rt-utils.c
@@ -534,7 +534,7 @@ void rt_test_start(void)
 	get_timestamp(ts_start);
 }
 
-void rt_write_json(const char *filename,
+void rt_write_json(const char *filename, int return_code,
 		  void (*cb)(FILE *, void *),
 		  void *data)
 {
@@ -572,6 +572,7 @@ void rt_write_json(const char *filename,
 	fprintf(f, "  \"rt_test_version:\": \"%1.2f\",\n", VERSION);
 	fprintf(f, "  \"start_time\": \"%s\",\n", ts_start);
 	fprintf(f, "  \"end_time\": \"%s\",\n", ts_end);
+	fprintf(f, "  \"return_code\": %d,\n", return_code);
 	fprintf(f, "  \"sysinfo\": {\n");
 	fprintf(f, "    \"sysname\": \"%s\",\n", uts.sysname);
 	fprintf(f, "    \"nodename\": \"%s\",\n", uts.nodename);
@@ -579,9 +580,13 @@ void rt_write_json(const char *filename,
 	fprintf(f, "    \"version\": \"%s\",\n", uts.version);
 	fprintf(f, "    \"machine\": \"%s\",\n", uts.machine);
 	fprintf(f, "    \"realtime\": %d\n", rt);
-	fprintf(f, "  },\n");
 
-	(cb)(f, data);
+	if (cb) {
+		fprintf(f, "  },\n");
+		(cb)(f, data);
+	} else {
+		fprintf(f, "  }\n");
+	}
 
 	fprintf(f, "}\n");
 
diff --git a/src/oslat/oslat.c b/src/oslat/oslat.c
index 2f02f5399405..37d528f0f7ff 100644
--- a/src/oslat/oslat.c
+++ b/src/oslat/oslat.c
@@ -863,7 +863,7 @@ int main(int argc, char *argv[])
 	write_summary(threads);
 
 	if (strlen(g.outfile) != 0)
-		rt_write_json(g.outfile, write_summary_json, threads);
+		rt_write_json(g.outfile, 0, write_summary_json, threads);
 
 	if (g.cpu_list) {
 		free(g.cpu_list);
diff --git a/src/pmqtest/pmqtest.c b/src/pmqtest/pmqtest.c
index adf72c10ce83..af10b416600c 100644
--- a/src/pmqtest/pmqtest.c
+++ b/src/pmqtest/pmqtest.c
@@ -652,7 +652,7 @@ int main(int argc, char *argv[])
 			.receiver = receiver,
 			.sender = sender,
 		};
-		rt_write_json(outfile, write_stats, &ps);
+		rt_write_json(outfile, 0, write_stats, &ps);
 	}
 
 nomem:
diff --git a/src/ptsematest/ptsematest.c b/src/ptsematest/ptsematest.c
index 471f1b307c14..70eb79e89635 100644
--- a/src/ptsematest/ptsematest.c
+++ b/src/ptsematest/ptsematest.c
@@ -520,7 +520,7 @@ int main(int argc, char *argv[])
 			.receiver = receiver,
 			.sender = sender,
 		};
-		rt_write_json(outfile, write_stats, &ps);
+		rt_write_json(outfile, 0, write_stats, &ps);
 	}
 
 nomem:
diff --git a/src/rt-migrate-test/rt-migrate-test.c b/src/rt-migrate-test/rt-migrate-test.c
index 8f628d347a23..1e0abdd6c0fa 100644
--- a/src/rt-migrate-test/rt-migrate-test.c
+++ b/src/rt-migrate-test/rt-migrate-test.c
@@ -664,7 +664,7 @@ int main (int argc, char **argv)
 	print_results();
 
 	if (strlen(outfile) != 0)
-		rt_write_json(outfile, write_stats, NULL);
+		rt_write_json(outfile, check < 0, write_stats, NULL);
 
 	if (stop) {
 		/*
diff --git a/src/sched_deadline/cyclicdeadline.c b/src/sched_deadline/cyclicdeadline.c
index a649e5a0fe5e..8d801b4c154a 100644
--- a/src/sched_deadline/cyclicdeadline.c
+++ b/src/sched_deadline/cyclicdeadline.c
@@ -1228,7 +1228,7 @@ int main(int argc, char **argv)
 	}
 
 	if (strlen(outfile) != 0)
-		rt_write_json(outfile, write_stats, sched_data);
+		rt_write_json(outfile, 0, write_stats, sched_data);
 
 	if (setcpu_buf)
 		free(setcpu_buf);
diff --git a/src/signaltest/signaltest.c b/src/signaltest/signaltest.c
index 7327d0d0144d..f43920e1a853 100644
--- a/src/signaltest/signaltest.c
+++ b/src/signaltest/signaltest.c
@@ -559,7 +559,7 @@ int main(int argc, char **argv)
 			free(stat[i].values);
 	}
 	if (strlen(outfile) != 0)
-		rt_write_json(outfile, write_stats, par);
+		rt_write_json(outfile, ret, write_stats, par);
 
 	free(stat);
  outpar:
diff --git a/src/sigwaittest/sigwaittest.c b/src/sigwaittest/sigwaittest.c
index 6e8bc7b50d5f..fda44724dd62 100644
--- a/src/sigwaittest/sigwaittest.c
+++ b/src/sigwaittest/sigwaittest.c
@@ -707,7 +707,7 @@ int main(int argc, char *argv[])
 			.receiver = receiver,
 			.sender = sender,
 		};
-		rt_write_json(outfile, write_stats, &ps);
+		rt_write_json(outfile, 0, write_stats, &ps);
 	}
 
 nomem:
diff --git a/src/svsematest/svsematest.c b/src/svsematest/svsematest.c
index 7533b1b569cc..9485ab6d95c8 100644
--- a/src/svsematest/svsematest.c
+++ b/src/svsematest/svsematest.c
@@ -779,7 +779,7 @@ int main(int argc, char *argv[])
 			.receiver = receiver,
 			.sender = sender,
 		};
-		rt_write_json(outfile, write_stats, &ps);
+		rt_write_json(outfile, 0, write_stats, &ps);
 	}
 
 nosem:
