Attach JSON Data
Attaching JSON data to a consultation is a common use case. You can convert the JSON data to CSV and then attach it to the consultation.
You will need to use the uploadMedia function and provide a Blob, file type, and file name.
This approach depends on an external library like json-2-csv to convert the JSON data to CSV; the CSV content is then wrapped in a browser Blob (no filesystem access is needed on web).
Attach JSON Data Example
import { json2csv } from 'json-2-csv';
import { uploadMedia } from 'react-web-altibbi';
const jsonData = [
{
extraData1: "value1",
extraData2: "value2",
extraData3: "value3",
},
];
const csvContent = json2csv(jsonData, {
excelBOM: true, // in case JSON data contains Arabic characters
});
const fileName = `attach-consultation-${new Date().getTime()}.csv`;
const file = new Blob([csvContent], { type: 'text/csv' });
uploadMedia(file, 'text/csv', fileName).then((res) => {
console.log(`JSON Media ID: ${res?.data?.id}`);
});