| 1 | # JSON formatter |
| 2 | |
| 3 | The CSV formatter presents [results of database queries](/src/web/api/queries/README.md) in the following formats: |
| 4 | |
| 5 | | format | content type | description| |
| 6 | |:----:|:----------:|:----------| |
| 7 | | `json` | application/json | return the query result as a json object| |
| 8 | | `jsonp` | application/json | return the query result as a JSONP javascript callback| |
| 9 | | `datatable` | application/json | return the query result as a Google `datatable`| |
| 10 | | `datasource` | application/json | return the query result as a Google Visualization Provider `datasource` javascript callback| |
| 11 | |
| 12 | The CSV formatter respects the following API `&options=`: |
| 13 | |
| 14 | | option | supported | description| |
| 15 | |:----:|:-------:|:----------| |
| 16 | | `google_json` | yes | enable the Google flavor of JSON (using double quotes for strings and `Date()` function for dates| |
| 17 | | `objectrows` | yes | return each row as an object, instead of an array| |
| 18 | | `nonzero` | yes | to return only the dimensions that have at least a non-zero value| |
| 19 | | `flip` | yes | to return the rows older to newer (the default is newer to older)| |
| 20 | | `seconds` | yes | to return the date and time in unix timestamp| |
| 21 | | `ms` | yes | to return the date and time in unit timestamp as milliseconds| |
| 22 | | `percent` | yes | to replace all values with their percentage over the row total| |
| 23 | | `abs` | yes | to turn all values positive| |
| 24 | | `null2zero` | yes | to replace gaps with zeros (the default prints the string `null`| |
| 25 | |
| 26 | ## Examples |
| 27 | |
| 28 | To show the differences between each format, in the following examples we query the same |
| 29 | chart (having just one dimension called `active`), changing only the query `format` and its `options`. |
| 30 | |
| 31 | > Using `format=json` and `options=` |
| 32 | |
| 33 | ```bash |
| 34 | # curl -Ss 'https://registry.my-netdata.io/api/v1/data?chart=nginx_local.connections&after=-3600&points=6&group=average&format=json&options=' |
| 35 | { |
| 36 | "labels": ["time", "active"], |
| 37 | "data": |
| 38 | [ |
| 39 | [ 1540644600, 224.2516667], |
| 40 | [ 1540644000, 229.29], |
| 41 | [ 1540643400, 222.41], |
| 42 | [ 1540642800, 226.6816667], |
| 43 | [ 1540642200, 246.4083333], |
| 44 | [ 1540641600, 241.0966667] |
| 45 | ] |
| 46 | } |
| 47 | ``` |
| 48 | |
| 49 | > Using `format=json` and `options=objectrows` |
| 50 | |
| 51 | ```bash |
| 52 | # curl -Ss 'https://registry.my-netdata.io/api/v1/data?chart=nginx_local.connections&after=-3600&points=6&group=average&format=json&options=objectrows' |
| 53 | { |
| 54 | "labels": ["time", "active"], |
| 55 | "data": |
| 56 | [ |
| 57 | { "time": 1540644600, "active": 224.2516667}, |
| 58 | { "time": 1540644000, "active": 229.29}, |
| 59 | { "time": 1540643400, "active": 222.41}, |
| 60 | { "time": 1540642800, "active": 226.6816667}, |
| 61 | { "time": 1540642200, "active": 246.4083333}, |
| 62 | { "time": 1540641600, "active": 241.0966667} |
| 63 | ] |
| 64 | } |
| 65 | ``` |
| 66 | |
| 67 | > Using `format=json` and `options=objectrows,google_json` |
| 68 | |
| 69 | ```bash |
| 70 | # curl -Ss 'https://registry.my-netdata.io/api/v1/data?chart=nginx_local.connections&after=-3600&points=6&group=average&formatjson&options=objectrows,google_json' |
| 71 | { |
| 72 | "labels": ["time", "active"], |
| 73 | "data": |
| 74 | [ |
| 75 | { "time": new Date(2018,9,27,12,50,0), "active": 224.2516667}, |
| 76 | { "time": new Date(2018,9,27,12,40,0), "active": 229.29}, |
| 77 | { "time": new Date(2018,9,27,12,30,0), "active": 222.41}, |
| 78 | { "time": new Date(2018,9,27,12,20,0), "active": 226.6816667}, |
| 79 | { "time": new Date(2018,9,27,12,10,0), "active": 246.4083333}, |
| 80 | { "time": new Date(2018,9,27,12,0,0), "active": 241.0966667} |
| 81 | ] |
| 82 | } |
| 83 | ``` |
| 84 | |
| 85 | > Using `format=jsonp` and `options=` |
| 86 | |
| 87 | ```bash |
| 88 | curl -Ss 'https://registry.my-netdata.io/api/v1/data?chart=nginx_local.connections&after=-3600&points=6&group=average&formjsonp&options=' |
| 89 | callback({ |
| 90 | "labels": ["time", "active"], |
| 91 | "data": |
| 92 | [ |
| 93 | [ 1540645200, 235.885], |
| 94 | [ 1540644600, 224.2516667], |
| 95 | [ 1540644000, 229.29], |
| 96 | [ 1540643400, 222.41], |
| 97 | [ 1540642800, 226.6816667], |
| 98 | [ 1540642200, 246.4083333] |
| 99 | ] |
| 100 | }); |
| 101 | ``` |
| 102 | |
| 103 | > Using `format=datatable` and `options=` |
| 104 | |
| 105 | ```bash |
| 106 | curl -Ss 'https://registry.my-netdata.io/api/v1/data?chart=nginx_local.connections&after=-3600&points=6&group=average&formdatatable&options=' |
| 107 | { |
| 108 | "cols": |
| 109 | [ |
| 110 | {"id":"","label":"time","pattern":"","type":"datetime"}, |
| 111 | {"id":"","label":"","pattern":"","type":"string","p":{"role":"annotation"}}, |
| 112 | {"id":"","label":"","pattern":"","type":"string","p":{"role":"annotationText"}}, |
| 113 | {"id":"","label":"active","pattern":"","type":"number"} |
| 114 | ], |
| 115 | "rows": |
| 116 | [ |
| 117 | {"c":[{"v":"Date(2018,9,27,13,0,0)"},{"v":null},{"v":null},{"v":235.885}]}, |
| 118 | {"c":[{"v":"Date(2018,9,27,12,50,0)"},{"v":null},{"v":null},{"v":224.2516667}]}, |
| 119 | {"c":[{"v":"Date(2018,9,27,12,40,0)"},{"v":null},{"v":null},{"v":229.29}]}, |
| 120 | {"c":[{"v":"Date(2018,9,27,12,30,0)"},{"v":null},{"v":null},{"v":222.41}]}, |
| 121 | {"c":[{"v":"Date(2018,9,27,12,20,0)"},{"v":null},{"v":null},{"v":226.6816667}]}, |
| 122 | {"c":[{"v":"Date(2018,9,27,12,10,0)"},{"v":null},{"v":null},{"v":246.4083333}]} |
| 123 | ] |
| 124 | } |
| 125 | ``` |
| 126 | |
| 127 | > Using `format=datasource` and `options=` |
| 128 | |
| 129 | ```bash |
| 130 | curl -Ss 'https://registry.my-netdata.io/api/v1/data?chart=nginx_local.connections&after=-3600&points=6&group=average&format=datasource&options=' |
| 131 | google.visualization.Query.setResponse({version:'0.6',reqId:'0',status:'ok',sig:'1540645368',table:{ |
| 132 | "cols": |
| 133 | [ |
| 134 | {"id":"","label":"time","pattern":"","type":"datetime"}, |
| 135 | {"id":"","label":"","pattern":"","type":"string","p":{"role":"annotation"}}, |
| 136 | {"id":"","label":"","pattern":"","type":"string","p":{"role":"annotationText"}}, |
| 137 | {"id":"","label":"active","pattern":"","type":"number"} |
| 138 | ], |
| 139 | "rows": |
| 140 | [ |
| 141 | {"c":[{"v":"Date(2018,9,27,13,0,0)"},{"v":null},{"v":null},{"v":235.885}]}, |
| 142 | {"c":[{"v":"Date(2018,9,27,12,50,0)"},{"v":null},{"v":null},{"v":224.2516667}]}, |
| 143 | {"c":[{"v":"Date(2018,9,27,12,40,0)"},{"v":null},{"v":null},{"v":229.29}]}, |
| 144 | {"c":[{"v":"Date(2018,9,27,12,30,0)"},{"v":null},{"v":null},{"v":222.41}]}, |
| 145 | {"c":[{"v":"Date(2018,9,27,12,20,0)"},{"v":null},{"v":null},{"v":226.6816667}]}, |
| 146 | {"c":[{"v":"Date(2018,9,27,12,10,0)"},{"v":null},{"v":null},{"v":246.4083333}]} |
| 147 | ] |
| 148 | }}); |
| 149 | ``` |
| 150 | |
| 151 |