Skip to main content

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 the file path, file type, and file name.

Also depending on external libraries like react-native-fs and json-2-csv to convert the JSON data to CSV.

Attach JSON Data Example

Check the example:

import { DownloadDirectoryPath, writeFile } from 'react-native-fs';
import { json2csv } from 'json-2-csv';
import { uploadMedia } from 'react-native-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 filePath = `${DownloadDirectoryPath}/${fileName}`;
await writeFile(filePath, csvContent, 'utf8');

uploadMedia(
`${Platform.OS === 'android' ? 'file://' : ''}` + filePath,
'text/csv',
fileName
).then((res) => {
console.log(`JSON Media ID: ${res?.data?.id}`);
});