MCPcopy Create free account
hub / github.com/Datakitpage/Datakit / parseJsonText

Function parseJsonText

frontend/src/hooks/stream/useStreamingJSONParser.ts:345–387  ·  view source on GitHub ↗
(text: string)

Source from the content-addressed store, hash-verified

343 * Parse a JSON text string directly (for small data)
344 */
345 const parseJsonText = (text: string): DataParseResult => {
346 try {
347 // Parse JSON text
348 let parsedJSON: any;
349
350 try {
351 parsedJSON = JSON.parse(text);
352 } catch (err) {
353 throw new Error(`Invalid JSON: ${err instanceof Error ? err.message : String(err)}`);
354 }
355
356 // Ensure we have an array to work with
357 const jsonArray = Array.isArray(parsedJSON)
358 ? parsedJSON
359 : [parsedJSON];
360
361 // Flatten JSON for tabular display
362 const [flattenedData, schema] = flattenJSON(jsonArray);
363
364 // Convert array of column types to the format expected by consumers
365 const columnTypes = flattenedData[0].map(header =>
366 schema.properties[header] || ColumnType.Text
367 );
368
369 // Prepare result
370 const result: DataParseResult = {
371 data: flattenedData,
372 columnTypes,
373 fileName: 'json-text',
374 rowCount: jsonArray.length,
375 columnCount: flattenedData[0].length,
376 sourceType: DataSourceType.JSON,
377 rawData: parsedJSON,
378 schema
379 };
380
381 return result;
382 } catch (err) {
383 const errorMessage = err instanceof Error ? err.message : 'Unknown error parsing JSON text';
384 setError(errorMessage);
385 throw err;
386 }
387 };
388
389 return {
390 parseJson,

Callers

nothing calls this directly

Calls 1

flattenJSONFunction · 0.85

Tested by

no test coverage detected