MCPcopy Index your code
hub / github.com/javascriptdata/danfojs / $toJSON

Function $toJSON

src/danfojs-base/io/node/io.json.ts:163–214  ·  view source on GitHub ↗
(df: NDframe | DataFrame | Series, options?: JsonOutputOptionsNode)

Source from the content-addressed store, hash-verified

161 * ```
162 */
163const $toJSON = (df: NDframe | DataFrame | Series, options?: JsonOutputOptionsNode): object | void => {
164 let { filePath, format } = { filePath: undefined, format: "column", ...options }
165
166 if (df.$isSeries) {
167 const obj: { [key: string]: ArrayType1D } = {};
168 obj[df.columns[0]] = df.values as ArrayType1D;
169 if (filePath) {
170 if (!filePath.endsWith(".json")) {
171 filePath = filePath + ".json"
172 }
173 fs.writeFileSync(filePath, JSON.stringify(obj))
174 } else {
175 return obj
176 }
177 } else {
178 const values = df.values as ArrayType2D
179 const header = df.columns
180 const jsonArr: any = [];
181
182 if (format === "row") {
183 const obj: { [key: string]: ArrayType1D } = {};
184 for (let i = 0; i < df.columns.length; i++) {
185 obj[df.columns[i]] = (df as DataFrame).column(df.columns[i]).values as ArrayType1D;
186 }
187 if (filePath !== undefined) {
188 if (!(filePath.endsWith(".json"))) {
189 filePath = filePath + ".json"
190 }
191
192 fs.writeFileSync(filePath, JSON.stringify(obj), "utf8")
193 } else {
194 return obj
195 }
196 } else {
197 values.forEach((val) => {
198 const obj: any = {};
199 header.forEach((h, i) => {
200 obj[h] = val[i]
201 });
202 jsonArr.push(obj);
203 });
204 if (filePath) {
205 if (!filePath.endsWith(".json")) {
206 filePath = filePath + ".json"
207 }
208 fs.writeFileSync(filePath, JSON.stringify(jsonArr))
209 } else {
210 return jsonArr
211 }
212 }
213 }
214};
215
216
217export {

Callers

nothing calls this directly

Calls 2

endsWithMethod · 0.80
columnMethod · 0.65

Tested by

no test coverage detected