MCPcopy Create free account
hub / github.com/javascriptdata/danfojs / $toJSON

Function $toJSON

src/danfojs-base/io/browser/io.json.ts:115–168  ·  view source on GitHub ↗
(df: NDframe | DataFrame | Series, options?: JsonOutputOptionsBrowser)

Source from the content-addressed store, hash-verified

113 * ```
114 */
115const $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.

Callers

nothing calls this directly

Calls 3

endsWithMethod · 0.80
$downloadFileInBrowserFunction · 0.70
columnMethod · 0.65

Tested by

no test coverage detected