(df: NDframe | DataFrame | Series, options?: JsonOutputOptionsBrowser)
| 113 | * ``` |
| 114 | */ |
| 115 | const $toJSON = (df: NDframe | DataFrame | Series, options?: JsonOutputOptionsBrowser): object | void => { |
| 116 | let { fileName, format, download } = { fileName: "output.json", download: false, format: "column", ...options } |
| 117 | |
| 118 | if (df.$isSeries) { |
| 119 | const obj: { [key: string]: ArrayType1D } = {}; |
| 120 | obj[df.columns[0]] = df.values as ArrayType1D; |
| 121 | |
| 122 | if (download) { |
| 123 | if (!fileName.endsWith(".json")) { |
| 124 | fileName = fileName + ".json" |
| 125 | } |
| 126 | $downloadFileInBrowser(obj, fileName) |
| 127 | } else { |
| 128 | return obj |
| 129 | } |
| 130 | |
| 131 | } else { |
| 132 | if (format === "row") { |
| 133 | const obj: { [key: string]: ArrayType1D } = {}; |
| 134 | for (let i = 0; i < df.columns.length; i++) { |
| 135 | obj[df.columns[i]] = (df as DataFrame).column(df.columns[i]).values as ArrayType1D; |
| 136 | } |
| 137 | if (download) { |
| 138 | if (!(fileName.endsWith(".json"))) { |
| 139 | fileName = fileName + ".json" |
| 140 | } |
| 141 | |
| 142 | $downloadFileInBrowser(obj, fileName) |
| 143 | } else { |
| 144 | return obj |
| 145 | } |
| 146 | } else { |
| 147 | const values = df.values as ArrayType2D |
| 148 | const header = df.columns |
| 149 | const jsonArr: any = []; |
| 150 | |
| 151 | values.forEach((val) => { |
| 152 | const obj: any = {}; |
| 153 | header.forEach((h, i) => { |
| 154 | obj[h] = val[i] |
| 155 | }); |
| 156 | jsonArr.push(obj); |
| 157 | }); |
| 158 | if (download) { |
| 159 | if (!fileName.endsWith(".json")) { |
| 160 | fileName = fileName + ".json" |
| 161 | } |
| 162 | $downloadFileInBrowser(jsonArr, fileName) |
| 163 | } else { |
| 164 | return jsonArr |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | /** |
| 171 | * Internal function to download a JSON file in the browser. |
nothing calls this directly
no test coverage detected