Woodshop
Search…
Import data from a JSON file
This example shows you how to implement a smart action to upload a JSON file. Here an activities model has a details field of the type JSON. This field should contain information from a JSON file.

Requirements

    An admin backend running on forest-express-sequelize

How it works

Directory: /models

This directory contains the activities.js file where the activities model is declared.
activities.js
1
module.exports = (sequelize, DataTypes) => {
2
const { Sequelize } = sequelize;
3
4
const Activities = sequelize.define('activities', {
5
details: {
6
type: DataTypes.JSONB,
7
},
8
startDate: {
9
type: DataTypes.DATE,
10
},
11
finishDate: {
12
type: DataTypes.DATE,
13
},
14
}, {
15
tableName: 'activities',
16
underscored: true,
17
timestamps: false,
18
schema: process.env.DATABASE_SCHEMA,
19
});
20
21
return Activities;
22
};
23
Copied!

Directory: /forest

This directory contains the activities.js file where the smart action Upload JSON is declared.
You need to specify that the widget file picker is applicable to the input field used to upload the file.
activities.js
1
const { collection } = require('forest-express-sequelize');
2
3
collection('activities', {
4
actions: [
5
{
6
name: 'Upload JSON',
7
type: 'single',
8
fields: [{
9
field: 'json',
10
widget: 'file picker',
11
}],
12
},
13
],
14
fields: [],
15
segments: [],
16
});
17
Copied!

Directory: /routes

This directory contains the activities.js file where the logic of the smart action is implemented.
activities.js
1
const express = require('express');
2
const { activities } = require('../models');
3
4
const router = express.Router();
5
6
router.post('/actions/upload-json', (request, response) => {
7
const [activityId] = new RecordsGetter(activities, req.user, req.query).getIdsFromRequest(request);
8
// Get the raw base64 file => if your field is a string and you want to insert the JSON as a base64 to use the file viewer, this is the value you want to save in the database
9
const rawFile = req.body.data.attributes.values.json;
10
// Trim the base64 string to delete the prefix
11
const rawFileCleaned = rawFile.replace('data:application/json;base64', '');
12
// Get json as string from base64 string
13
const stringFile = Buffer.from(rawFileCleaned, 'base64').toString('utf8');
14
// Check that you can properly parse json from the string obtained
15
16
let jsonFile
17
try {
18
jsonFile = JSON.parse(stringFile);
19
} catch (error) {
20
return res.status(400).send({ error: 'not a correctly formatted json file' });
21
}
22
23
// Find and update the current record's details field with the json file as a string
24
return activities.update({
25
details: jsonFile
26
},{
27
where: {
28
id: activityId,
29
},
30
})
31
.then(() => {
32
return res.send({ success: 'record updated!' });
33
})
34
.catch((e) => {
35
console.error(e);
36
return res.status(400).send({ error: 'could not update file' });
37
});
38
});
39
40
module.exports = router;
Copied!
Last modified 2mo ago