[rrd-developers] PATCH: Fix for JSON output format

Thomas Mainka thomas.mainka at gmx.de
Wed Sep 5 22:32:44 CEST 2012


Hi everyone,


Recently I have experimented with dynamic graphing of RRDtool data on 
web pages (as JSON data queried by jQuery). While the JSON export and 
transport worked fine, after some testing I realized that starting with 
version 1.4 jQuery rejects JSON data which it thinks is malformed. When 
directly fed the output output of "rrdtool xport --json" jQuery barfs 
with an syntax error.

Of course there are workarounds, but the attached fix is pretty trivial: 
Only the double quotes were missing in most of the JSON output from 
RRDtool to pass the jQuery test. The JSON standard is pretty strict on 
strings and requires any string to be wrapped in double quotes: 
http://json.org/

Personally I have also tested the resulting output with 
http://jsonlint.org/ and http://www.jsoneditoronline.org/ which both 
worked for me. YMMV, though.


Regards,
Thomas
-------------- next part --------------
--- ../rrdtool-1.4.7.orig/src/rrd_tool.c	2012-01-24 11:08:48.000000000 +0100
+++ src/rrd_tool.c	2012-09-05 17:11:34.471626553 +0200
@@ -737,13 +737,13 @@
                 printf("  <%s>\n", META_TAG);
             }
             else {
-                printf("{ about: 'RRDtool xport JSON output',\n  meta: {\n");
+                printf("{ \"about\": \"RRDtool xport JSON output\",\n  \"meta\": {\n");
             }
 
 
 #define pXJV(indent,fmt,tag,value) \
             if (json) { \
-               printf(indent "%s: " fmt ",\n",tag,value); \
+               printf(indent "\"%s\": " fmt ",\n",tag,value); \
             } else { \
                printf(indent "<%s>" fmt "</%s>\n",tag,value,tag); \
             }
@@ -757,7 +757,7 @@
             }
              
             if (json){
-                printf("    %s: [\n", LEGEND_TAG);
+                printf("    \"%s\": [\n", LEGEND_TAG);
             }
             else {
                 printf("    <%s>\n", LEGEND_TAG);
@@ -766,7 +766,7 @@
                 char     *entry = NULL;
                 entry = legend_v[j];
                 if (json){
-                    printf("      '%s'", entry);
+                    printf("      \"%s\"", entry);
                     if (j < col_cnt -1){
                         printf(",");
                     }
@@ -788,7 +788,7 @@
             }
             
             if (json){
-                printf("  %s: [\n",DATA_TAG);
+                printf("  \"%s\": [\n",DATA_TAG);
             } else {
                 printf("  <%s>\n", DATA_TAG);
             }


More information about the rrd-developers mailing list