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

Function $readJSON

src/danfojs-base/io/browser/io.json.ts:50–80  ·  view source on GitHub ↗
(file: any, options?: JsonInputOptionsBrowser)

Source from the content-addressed store, hash-verified

48 * ```
49 */
50const $readJSON = async (file: any, options?: JsonInputOptionsBrowser) => {
51 const { method, headers, frameConfig } = { method: "GET", headers: {}, frameConfig: {}, ...options }
52
53 if (typeof file === "string" && file.startsWith("http")) {
54
55 return new Promise(resolve => {
56 fetch(file, { method, headers }).then(response => {
57 if (response.status !== 200) {
58 throw new Error(`Failed to load ${file}`)
59 }
60 response.json().then(json => {
61 resolve(new DataFrame(json, frameConfig));
62 });
63 }).catch((err) => {
64 throw new Error(err)
65 })
66 })
67
68 } else if (file instanceof File) {
69 return new Promise(resolve => {
70 const reader = new FileReader();
71 reader.readAsText(file);
72 reader.onload = (event) => {
73 const jsonObj = JSON.parse(event?.target?.result as string);
74 resolve(new DataFrame(jsonObj, frameConfig));
75 }
76 })
77 } else {
78 throw new Error("ParamError: File not supported. file must be a url or an input File object")
79 }
80};
81
82
83/**

Callers

nothing calls this directly

Calls 1

startsWithMethod · 0.80

Tested by

no test coverage detected